Github user ilooner commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45116702
--- Diff:
library/src/main/java/com/datatorrent/lib/appdata/gpo/GPOUtils.java ---
@@ -178,33 +226,44 @@ public static void setFieldFromJSON(GPOMutable gpo,
Type type, String field, JSO
*/
public static void setFieldFromJSON(GPOMutable gpo, String field,
JSONObject jo)
{
- Type type = gpo.getFieldDescriptor().getType(field);
+ Object val = getFieldFromJSON(gpo.getFieldDescriptor(), field, jo);
+ gpo.setFieldGeneric(field, val);
+ }
+
+ /**
+ * This method gets the given field from the given {@link JSONObject}
and converts the field to an object
+ * of the type specified in the given {@link FieldsDescriptor}.
+ * @param fd The {@link FieldsDescriptor} describing the type of each
field.
+ * @param field The field to retrieve from the given {@link JSONObject}.
+ * @param jo The {@link JSONObject} to retrieve a field from.
+ * @return The value of the given field converted to an object of the
correct type.
+ */
+ public static Object getFieldFromJSON(FieldsDescriptor fd, String field,
JSONObject jo)
+ {
+ Type type = fd.getType(field);
- if(type == Type.BOOLEAN) {
+ if (type == Type.BOOLEAN) {
Boolean val;
try {
val = jo.getBoolean(field);
- }
- catch(JSONException ex) {
+ } catch (JSONException ex) {
throw new IllegalArgumentException("The key " + field + " does not
have a valid bool value.", ex);
}
- gpo.setFieldGeneric(field, val);
- }
- else if(type == Type.BYTE) {
+ return val;
+ } else if (type == Type.BYTE) {
int val;
try {
val = jo.getInt(field);
- }
- catch(JSONException ex) {
+ } catch (JSONException ex) {
--- 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.
---