Github user gauravgopi123 commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45100705
--- Diff:
library/src/main/java/com/datatorrent/lib/appdata/gpo/GPOUtils.java ---
@@ -304,52 +355,216 @@ else if(type == Type.CHAR) {
+ " that is not one character
long.");
}
- gpo.setField(field, val.charAt(0));
- }
- else if(type == Type.STRING) {
+ return val.charAt(0);
+ } else if (type == Type.STRING) {
String val;
try {
val = jo.getString(field);
- }
- catch(JSONException ex) {
+ } catch (JSONException ex) {
throw new IllegalArgumentException("The key "
+ field
+ " does not have a valid
string value.",
ex);
}
- gpo.setField(field, val);
- }
- else if(type == Type.DOUBLE) {
+ return val;
+ } else if (type == Type.DOUBLE) {
Double val;
try {
val = jo.getDouble(field);
- }
- catch(JSONException ex) {
+ } catch (JSONException ex) {
throw new IllegalArgumentException("The key "
+ field
+ " does not have a valid
double value.",
ex);
}
- gpo.setFieldGeneric(field, val);
- }
- else if(type == Type.FLOAT) {
+ return val;
+ } else if (type == Type.FLOAT) {
Float val;
try {
val = (float)jo.getDouble(field);
- }
- catch(JSONException ex) {
+ } catch (JSONException ex) {
throw new IllegalArgumentException("The key "
+ field
+ " does not have a valid
double value.",
ex);
}
- gpo.setFieldGeneric(field, val);
+ return val;
+ } else {
+ throw new UnsupportedOperationException("The type " + type + " is
not supported.");
+ }
+ }
+
+ /**
+ * This method gets an object of the given {@link Type} from the given
{@link JSONArray} at the
+ * given index.
+ * @param type The {@link Type} of the object to retrieve from the
{@link JSONArray}.
+ * @param ja The {@link JSONArray} to retrieve objects from.
+ * @param index The index of the object in the {@link JSONArray} to
retrieve.
+ * @return The object retrieved from the {@link JSONArray}.
+ */
+ public static Object getFieldFromJSON(Type type, JSONArray ja, int index)
+ {
+ if (type == Type.BOOLEAN) {
+ Boolean val;
+
+ try {
+ val = ja.getBoolean(index);
+ } catch (JSONException ex) {
+ throw new IllegalArgumentException("The index " + index + " does
not have a valid bool value.", ex);
+ }
+
+ return val;
+ } else if (type == Type.BYTE) {
+ int val;
+
+ try {
+ val = ja.getInt(index);
+ } catch (JSONException ex) {
+ throw new IllegalArgumentException("The index "
+ + index
+ + " does not have a valid byte
value.", ex);
+ }
+
+ if (val < (int)Byte.MIN_VALUE) {
+ throw new IllegalArgumentException("The index "
+ + index
+ + " has a value "
+ + val
+ + " which is too small to fit
into a byte.");
+ }
+
+ if (val > (int)Byte.MAX_VALUE) {
+ throw new IllegalArgumentException("The index "
+ + index
+ + " has a value "
+ + val
+ + " which is too larg to fit
into a byte.");
+ }
+
+ return ((byte)val);
+ } else if (type == Type.SHORT) {
+ int val;
+
+ try {
+ val = ja.getInt(index);
+ } catch (JSONException ex) {
+ throw new IllegalArgumentException("The index "
+ + index
+ + " does not have a valid short
value.",
+ ex);
+ }
+
+ if (val < (int)Short.MIN_VALUE) {
+ throw new IllegalArgumentException("The index "
+ + index
+ + " has a value "
+ + val
+ + " which is too small to fit
into a short.");
+ }
+
+ if (val > (int)Short.MAX_VALUE) {
+ throw new IllegalArgumentException("The index "
+ + index
+ + " has a value "
+ + val
+ + " which is too large to fit
into a short.");
+ }
+
+ return ((short)val);
+ } else if (type == Type.INTEGER) {
+ int val;
+
+ try {
+ val = ja.getInt(index);
+ } catch (JSONException ex) {
+ throw new IllegalArgumentException("The index "
+ + index
+ + " does not have a valid int
value.",
+ ex);
+ }
+
+ return val;
+ } else if (type == Type.LONG) {
+ long val;
+
+ try {
+ val = ja.getLong(index);
+ } catch (JSONException ex) {
+ throw new IllegalArgumentException("The index "
+ + index
+ + " does not have a valid long
value.",
+ ex);
+ }
+
+ return val;
+ } else if (type == Type.CHAR) {
+ String val;
+
+ try {
+ val = ja.getString(index);
+ } catch (JSONException ex) {
+ throw new IllegalArgumentException("The index "
+ + index
+ + " does not have a valid
character value.",
+ ex);
+ }
+
+ if (val.length() != 1) {
+ throw new IllegalArgumentException("The index "
+ + index
+ + " has a value "
+ + val
+ + " that is not one character
long.");
+ }
+
+ return val.charAt(0);
+ } else if (type == Type.STRING) {
+ String val;
+
+ try {
+ val = ja.getString(index);
+ } catch (JSONException ex) {
+ throw new IllegalArgumentException("The index "
+ + index
+ + " does not have a valid
string value.",
+ ex);
+ }
+
+ return val;
+ } else if (type == Type.DOUBLE) {
+ Double val;
+
+ try {
+ val = ja.getDouble(index);
+ } catch (JSONException ex) {
+ throw new IllegalArgumentException("The index "
+ + index
+ + " does not have a valid
double value.",
+ ex);
+ }
+
+ return val;
+ } else if (type == Type.FLOAT) {
+ Float val;
+
+ try {
+ val = (float) ja.getDouble(index);
--- End diff --
same as above
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---