You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apex.apache.org by ilooner <gi...@git.apache.org> on 2015/11/17 19:30:46 UTC

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

GitHub user ilooner opened a pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103

    - MLHR-1908 Added helper method to deserialize JSONObject into a Map.

    

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/ilooner/incubator-apex-malhar MLHR-1908

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/incubator-apex-malhar/pull/103.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #103
    
----
commit d3b994b2cb343756b66a58e9b6bd593341fbba83
Author: Timothy Farkas <ti...@datatorrent.com>
Date:   2015-11-17T18:29:12Z

    - MLHR-1908 Added helper method to deserialize JSONObject into a Map.

----


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by gauravgopi123 <gi...@git.apache.org>.
Github user gauravgopi123 commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45099726
  
    --- Diff: library/src/main/java/com/datatorrent/lib/appdata/gpo/GPOUtils.java ---
    @@ -123,6 +123,54 @@ public static GPOMutable deserialize(FieldsDescriptor fieldsDescriptor,
       }
     
       /**
    +   * This method deserializes the fields in the given {@link FieldsDescriptor} into a map.
    +   * @param fieldsDescriptor The {@link FieldsDescriptor} to fetch fields from.
    +   * @param dpou The {@link JSONObject} which contains the fields whose values need to be fetched.
    +   * @return A {@link Map} whose keys are field names, and whose values are possible values for those fields.
    +   */
    +  public static Map<String, Set<Object>> deserializeToMap(FieldsDescriptor fieldsDescriptor,
    +                                                          JSONObject dpou)
    +  {
    +    Map<String, Set<Object>> keyToValues = Maps.newHashMap();
    +
    +    for (String key : fieldsDescriptor.getFields().getFields()) {
    +      if (!dpou.has(key)) {
    +        throw new IllegalArgumentException("The given key " + key + " is not contained in the given JSON");
    +      }
    +
    +      Type type = fieldsDescriptor.getType(key);
    +      Set<Object> keyValues = Sets.newHashSet();
    +      keyToValues.put(key, keyValues);
    +
    +      Object keyValue;
    +
    +      try {
    +        keyValue = dpou.get(key);
    +      } catch (JSONException ex) {
    +        throw new IllegalStateException("This should never happen", ex);
    +      }
    +
    +      if (keyValue instanceof JSONArray) {
    +
    +        JSONArray ja = (JSONArray) keyValue;
    +
    +        for (int index = 0; index < ja.length(); index++) {
    +          Object val = getFieldFromJSON(type, ja, index);
    +          keyValues.add(val);
    +        }
    +
    +      } else if (keyValue instanceof JSONObject) {
    +        throw new UnsupportedOperationException("Cannot extract objects from JSONObjects");
    +      } else {
    +        Object val = getFieldFromJSON(fieldsDescriptor, key, dpou);
    +        keyValues.add(val);
    --- End diff --
    
    same.. why not keyValues.add(getFieldFromJSON(fieldsDescriptor, key, dpou));


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by gauravgopi123 <gi...@git.apache.org>.
Github user gauravgopi123 commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45104979
  
    --- 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 --
    
    why catch exception?



---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by gauravgopi123 <gi...@git.apache.org>.
Github user gauravgopi123 commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45103071
  
    --- Diff: library/src/main/java/com/datatorrent/lib/appdata/gpo/GPOUtils.java ---
    @@ -123,6 +123,54 @@ public static GPOMutable deserialize(FieldsDescriptor fieldsDescriptor,
       }
     
       /**
    +   * This method deserializes the fields in the given {@link FieldsDescriptor} into a map.
    +   * @param fieldsDescriptor The {@link FieldsDescriptor} to fetch fields from.
    +   * @param dpou The {@link JSONObject} which contains the fields whose values need to be fetched.
    +   * @return A {@link Map} whose keys are field names, and whose values are possible values for those fields.
    +   */
    +  public static Map<String, Set<Object>> deserializeToMap(FieldsDescriptor fieldsDescriptor,
    +                                                          JSONObject dpou)
    +  {
    +    Map<String, Set<Object>> keyToValues = Maps.newHashMap();
    +
    +    for (String key : fieldsDescriptor.getFields().getFields()) {
    +      if (!dpou.has(key)) {
    +        throw new IllegalArgumentException("The given key " + key + " is not contained in the given JSON");
    +      }
    +
    +      Type type = fieldsDescriptor.getType(key);
    +      Set<Object> keyValues = Sets.newHashSet();
    --- End diff --
    
    I think you should not initialize it here. 
    In if (keyValue instanceof JSONArray){} loop , initialized it with length of the array and in final else loop initialize it with length 1 using Sets.newHashSetWithExpectedSize(). 
    
    keyToValues.put(key, keyValues) can be the last step in main for loop


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by ilooner <gi...@git.apache.org>.
Github user ilooner commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45114790
  
    --- 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);
    --- End diff --
    
    Done


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by gauravgopi123 <gi...@git.apache.org>.
Github user gauravgopi123 commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45100533
  
    --- 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);
    --- End diff --
    
    return ja.getString(index);


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by ilooner <gi...@git.apache.org>.
Github user ilooner commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45115317
  
    --- 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 --
    
    Done


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by ilooner <gi...@git.apache.org>.
Github user ilooner commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45114233
  
    --- Diff: library/src/main/java/com/datatorrent/lib/appdata/gpo/GPOUtils.java ---
    @@ -123,6 +123,54 @@ public static GPOMutable deserialize(FieldsDescriptor fieldsDescriptor,
       }
     
       /**
    +   * This method deserializes the fields in the given {@link FieldsDescriptor} into a map.
    +   * @param fieldsDescriptor The {@link FieldsDescriptor} to fetch fields from.
    +   * @param dpou The {@link JSONObject} which contains the fields whose values need to be fetched.
    +   * @return A {@link Map} whose keys are field names, and whose values are possible values for those fields.
    +   */
    +  public static Map<String, Set<Object>> deserializeToMap(FieldsDescriptor fieldsDescriptor,
    +                                                          JSONObject dpou)
    +  {
    +    Map<String, Set<Object>> keyToValues = Maps.newHashMap();
    +
    +    for (String key : fieldsDescriptor.getFields().getFields()) {
    +      if (!dpou.has(key)) {
    +        throw new IllegalArgumentException("The given key " + key + " is not contained in the given JSON");
    +      }
    +
    +      Type type = fieldsDescriptor.getType(key);
    --- End diff --
    
    Done


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by gauravgopi123 <gi...@git.apache.org>.
Github user gauravgopi123 commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45105031
  
    --- 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) {
    --- End diff --
    
    why catch exception?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/incubator-apex-malhar/pull/103


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by ilooner <gi...@git.apache.org>.
Github user ilooner commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45114086
  
    --- Diff: library/src/main/java/com/datatorrent/lib/appdata/gpo/GPOUtils.java ---
    @@ -123,6 +123,54 @@ public static GPOMutable deserialize(FieldsDescriptor fieldsDescriptor,
       }
     
       /**
    +   * This method deserializes the fields in the given {@link FieldsDescriptor} into a map.
    +   * @param fieldsDescriptor The {@link FieldsDescriptor} to fetch fields from.
    +   * @param dpou The {@link JSONObject} which contains the fields whose values need to be fetched.
    +   * @return A {@link Map} whose keys are field names, and whose values are possible values for those fields.
    +   */
    +  public static Map<String, Set<Object>> deserializeToMap(FieldsDescriptor fieldsDescriptor,
    +                                                          JSONObject dpou)
    +  {
    +    Map<String, Set<Object>> keyToValues = Maps.newHashMap();
    +
    +    for (String key : fieldsDescriptor.getFields().getFields()) {
    +      if (!dpou.has(key)) {
    +        throw new IllegalArgumentException("The given key " + key + " is not contained in the given JSON");
    +      }
    +
    +      Type type = fieldsDescriptor.getType(key);
    +      Set<Object> keyValues = Sets.newHashSet();
    +      keyToValues.put(key, keyValues);
    +
    +      Object keyValue;
    +
    +      try {
    +        keyValue = dpou.get(key);
    +      } catch (JSONException ex) {
    +        throw new IllegalStateException("This should never happen", ex);
    +      }
    +
    +      if (keyValue instanceof JSONArray) {
    +
    +        JSONArray ja = (JSONArray) keyValue;
    +
    +        for (int index = 0; index < ja.length(); index++) {
    +          Object val = getFieldFromJSON(type, ja, index);
    +          keyValues.add(val);
    +        }
    +
    +      } else if (keyValue instanceof JSONObject) {
    +        throw new UnsupportedOperationException("Cannot extract objects from JSONObjects");
    +      } else {
    +        Object val = getFieldFromJSON(fieldsDescriptor, key, dpou);
    +        keyValues.add(val);
    --- End diff --
    
    Done


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by ilooner <gi...@git.apache.org>.
Github user ilooner commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45114310
  
    --- 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);
    --- End diff --
    
    Done


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by gauravgopi123 <gi...@git.apache.org>.
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by gauravgopi123 <gi...@git.apache.org>.
Github user gauravgopi123 commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45100224
  
    --- 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);
    --- End diff --
    
    return ja.getBoolean(index);


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by ilooner <gi...@git.apache.org>.
Github user ilooner commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45115203
  
    --- 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);
    --- End diff --
    
    Done


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by ilooner <gi...@git.apache.org>.
Github user ilooner commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45114891
  
    --- 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);
    --- End diff --
    
    Done


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by ilooner <gi...@git.apache.org>.
Github user ilooner commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45116148
  
    --- Diff: library/src/main/java/com/datatorrent/lib/appdata/gpo/GPOUtils.java ---
    @@ -123,6 +123,54 @@ public static GPOMutable deserialize(FieldsDescriptor fieldsDescriptor,
       }
     
       /**
    +   * This method deserializes the fields in the given {@link FieldsDescriptor} into a map.
    +   * @param fieldsDescriptor The {@link FieldsDescriptor} to fetch fields from.
    +   * @param dpou The {@link JSONObject} which contains the fields whose values need to be fetched.
    +   * @return A {@link Map} whose keys are field names, and whose values are possible values for those fields.
    +   */
    +  public static Map<String, Set<Object>> deserializeToMap(FieldsDescriptor fieldsDescriptor,
    +                                                          JSONObject dpou)
    +  {
    +    Map<String, Set<Object>> keyToValues = Maps.newHashMap();
    +
    +    for (String key : fieldsDescriptor.getFields().getFields()) {
    +      if (!dpou.has(key)) {
    +        throw new IllegalArgumentException("The given key " + key + " is not contained in the given JSON");
    +      }
    +
    +      Type type = fieldsDescriptor.getType(key);
    +      Set<Object> keyValues = Sets.newHashSet();
    --- End diff --
    
    Done


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by ilooner <gi...@git.apache.org>.
Github user ilooner commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45116680
  
    --- Diff: library/src/main/java/com/datatorrent/lib/appdata/gpo/GPOUtils.java ---
    @@ -123,6 +123,54 @@ public static GPOMutable deserialize(FieldsDescriptor fieldsDescriptor,
       }
     
       /**
    +   * This method deserializes the fields in the given {@link FieldsDescriptor} into a map.
    +   * @param fieldsDescriptor The {@link FieldsDescriptor} to fetch fields from.
    +   * @param dpou The {@link JSONObject} which contains the fields whose values need to be fetched.
    +   * @return A {@link Map} whose keys are field names, and whose values are possible values for those fields.
    +   */
    +  public static Map<String, Set<Object>> deserializeToMap(FieldsDescriptor fieldsDescriptor,
    +                                                          JSONObject dpou)
    +  {
    +    Map<String, Set<Object>> keyToValues = Maps.newHashMap();
    +
    +    for (String key : fieldsDescriptor.getFields().getFields()) {
    +      if (!dpou.has(key)) {
    +        throw new IllegalArgumentException("The given key " + key + " is not contained in the given JSON");
    +      }
    +
    +      Type type = fieldsDescriptor.getType(key);
    +      Set<Object> keyValues = Sets.newHashSet();
    +      keyToValues.put(key, keyValues);
    +
    +      Object keyValue;
    +
    +      try {
    +        keyValue = dpou.get(key);
    +      } catch (JSONException ex) {
    --- End diff --
    
    This is a convenience method for handling JSON. it is not convenient to do exception handling every time this method is called. It also doesn't enable you to do anything useful.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by gauravgopi123 <gi...@git.apache.org>.
Github user gauravgopi123 commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45100464
  
    --- 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);
    --- End diff --
    
    same return ja.getLong(index);


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by gauravgopi123 <gi...@git.apache.org>.
Github user gauravgopi123 commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45099610
  
    --- Diff: library/src/main/java/com/datatorrent/lib/appdata/gpo/GPOUtils.java ---
    @@ -123,6 +123,54 @@ public static GPOMutable deserialize(FieldsDescriptor fieldsDescriptor,
       }
     
       /**
    +   * This method deserializes the fields in the given {@link FieldsDescriptor} into a map.
    +   * @param fieldsDescriptor The {@link FieldsDescriptor} to fetch fields from.
    +   * @param dpou The {@link JSONObject} which contains the fields whose values need to be fetched.
    +   * @return A {@link Map} whose keys are field names, and whose values are possible values for those fields.
    +   */
    +  public static Map<String, Set<Object>> deserializeToMap(FieldsDescriptor fieldsDescriptor,
    +                                                          JSONObject dpou)
    +  {
    +    Map<String, Set<Object>> keyToValues = Maps.newHashMap();
    +
    +    for (String key : fieldsDescriptor.getFields().getFields()) {
    +      if (!dpou.has(key)) {
    +        throw new IllegalArgumentException("The given key " + key + " is not contained in the given JSON");
    +      }
    +
    +      Type type = fieldsDescriptor.getType(key);
    +      Set<Object> keyValues = Sets.newHashSet();
    +      keyToValues.put(key, keyValues);
    +
    +      Object keyValue;
    +
    +      try {
    +        keyValue = dpou.get(key);
    +      } catch (JSONException ex) {
    +        throw new IllegalStateException("This should never happen", ex);
    +      }
    +
    +      if (keyValue instanceof JSONArray) {
    +
    +        JSONArray ja = (JSONArray) keyValue;
    +
    +        for (int index = 0; index < ja.length(); index++) {
    +          Object val = getFieldFromJSON(type, ja, index);
    +          keyValues.add(val);
    --- End diff --
    
    why not keyValues.add(getFieldFromJSON(type, ja, index));


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by gauravgopi123 <gi...@git.apache.org>.
Github user gauravgopi123 commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45100379
  
    --- 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);
    --- End diff --
    
    return ja.getInt(index);


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by brightchen <gi...@git.apache.org>.
Github user brightchen commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45140443
  
    --- Diff: library/src/main/java/com/datatorrent/lib/appdata/gpo/GPOUtils.java ---
    @@ -178,33 +227,40 @@ 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);
    +  }
     
    -    if(type == Type.BOOLEAN) {
    -      Boolean 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) {
           try {
    -        val = jo.getBoolean(field);
    -      }
    -      catch(JSONException ex) {
    +        return jo.getBoolean(field);
    +      } 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) {
    +    } else if (type == Type.BYTE) {
           int val;
     
           try {
             val = jo.getInt(field);
    -      }
    -      catch(JSONException ex) {
    +      } catch (JSONException ex) {
             throw new IllegalArgumentException("The key "
                                                + field
                                                + " does not have a valid byte value.", ex);
           }
     
    -      if(val < (int)Byte.MIN_VALUE) {
    +      if (val < (int)Byte.MIN_VALUE) {
    --- End diff --
    
    what about combine these two logic into one and throw IllegalArgumentException with "Out of range". 


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by ilooner <gi...@git.apache.org>.
Github user ilooner commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45114046
  
    --- Diff: library/src/main/java/com/datatorrent/lib/appdata/gpo/GPOUtils.java ---
    @@ -123,6 +123,54 @@ public static GPOMutable deserialize(FieldsDescriptor fieldsDescriptor,
       }
     
       /**
    +   * This method deserializes the fields in the given {@link FieldsDescriptor} into a map.
    +   * @param fieldsDescriptor The {@link FieldsDescriptor} to fetch fields from.
    +   * @param dpou The {@link JSONObject} which contains the fields whose values need to be fetched.
    +   * @return A {@link Map} whose keys are field names, and whose values are possible values for those fields.
    +   */
    +  public static Map<String, Set<Object>> deserializeToMap(FieldsDescriptor fieldsDescriptor,
    +                                                          JSONObject dpou)
    +  {
    +    Map<String, Set<Object>> keyToValues = Maps.newHashMap();
    +
    +    for (String key : fieldsDescriptor.getFields().getFields()) {
    +      if (!dpou.has(key)) {
    +        throw new IllegalArgumentException("The given key " + key + " is not contained in the given JSON");
    +      }
    +
    +      Type type = fieldsDescriptor.getType(key);
    +      Set<Object> keyValues = Sets.newHashSet();
    +      keyToValues.put(key, keyValues);
    +
    +      Object keyValue;
    +
    +      try {
    +        keyValue = dpou.get(key);
    +      } catch (JSONException ex) {
    +        throw new IllegalStateException("This should never happen", ex);
    +      }
    +
    +      if (keyValue instanceof JSONArray) {
    +
    +        JSONArray ja = (JSONArray) keyValue;
    +
    +        for (int index = 0; index < ja.length(); index++) {
    +          Object val = getFieldFromJSON(type, ja, index);
    +          keyValues.add(val);
    --- End diff --
    
    Done


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by ilooner <gi...@git.apache.org>.
Github user ilooner commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45115291
  
    --- 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);
    --- End diff --
    
    Done


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by brightchen <gi...@git.apache.org>.
Github user brightchen commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45141784
  
    --- Diff: library/src/main/java/com/datatorrent/lib/appdata/gpo/GPOUtils.java ---
    @@ -178,33 +227,40 @@ 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);
    +  }
     
    -    if(type == Type.BOOLEAN) {
    -      Boolean 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)
    --- End diff --
    
    maybe can combine the logic of integer numeric( byte, short, int ). such as
    if(isIntegerNumeric(type))
    {
      int val = jo.getInt(field);
      if( !insideRange(val, type) )
         throw new IllegalArgumentException( "value {} out of range of type {}" );
    } 


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by gauravgopi123 <gi...@git.apache.org>.
Github user gauravgopi123 commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45099965
  
    --- Diff: library/src/main/java/com/datatorrent/lib/appdata/gpo/GPOUtils.java ---
    @@ -123,6 +123,54 @@ public static GPOMutable deserialize(FieldsDescriptor fieldsDescriptor,
       }
     
       /**
    +   * This method deserializes the fields in the given {@link FieldsDescriptor} into a map.
    +   * @param fieldsDescriptor The {@link FieldsDescriptor} to fetch fields from.
    +   * @param dpou The {@link JSONObject} which contains the fields whose values need to be fetched.
    +   * @return A {@link Map} whose keys are field names, and whose values are possible values for those fields.
    +   */
    +  public static Map<String, Set<Object>> deserializeToMap(FieldsDescriptor fieldsDescriptor,
    +                                                          JSONObject dpou)
    +  {
    +    Map<String, Set<Object>> keyToValues = Maps.newHashMap();
    +
    +    for (String key : fieldsDescriptor.getFields().getFields()) {
    +      if (!dpou.has(key)) {
    +        throw new IllegalArgumentException("The given key " + key + " is not contained in the given JSON");
    +      }
    +
    +      Type type = fieldsDescriptor.getType(key);
    --- End diff --
    
    Can this go into if (keyValue instanceof JSONArray) {} loop


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by ilooner <gi...@git.apache.org>.
Github user ilooner commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45116727
  
    --- 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) {
    --- 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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by ilooner <gi...@git.apache.org>.
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by gauravgopi123 <gi...@git.apache.org>.
Github user gauravgopi123 commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45103964
  
    --- 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) {
    --- End diff --
    
    why not return jo.getBoolean(field); 
    
    This also should be done for LONG, INTEGER, DOUBLE etc


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by gauravgopi123 <gi...@git.apache.org>.
Github user gauravgopi123 commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45160796
  
    --- Diff: library/src/main/java/com/datatorrent/lib/appdata/gpo/GPOUtils.java ---
    @@ -123,6 +123,54 @@ public static GPOMutable deserialize(FieldsDescriptor fieldsDescriptor,
       }
     
       /**
    +   * This method deserializes the fields in the given {@link FieldsDescriptor} into a map.
    +   * @param fieldsDescriptor The {@link FieldsDescriptor} to fetch fields from.
    +   * @param dpou The {@link JSONObject} which contains the fields whose values need to be fetched.
    +   * @return A {@link Map} whose keys are field names, and whose values are possible values for those fields.
    +   */
    +  public static Map<String, Set<Object>> deserializeToMap(FieldsDescriptor fieldsDescriptor,
    +                                                          JSONObject dpou)
    +  {
    +    Map<String, Set<Object>> keyToValues = Maps.newHashMap();
    +
    +    for (String key : fieldsDescriptor.getFields().getFields()) {
    +      if (!dpou.has(key)) {
    +        throw new IllegalArgumentException("The given key " + key + " is not contained in the given JSON");
    +      }
    +
    +      Type type = fieldsDescriptor.getType(key);
    +      Set<Object> keyValues = Sets.newHashSet();
    +      keyToValues.put(key, keyValues);
    +
    +      Object keyValue;
    +
    +      try {
    +        keyValue = dpou.get(key);
    +      } catch (JSONException ex) {
    --- End diff --
    
    This is util class and it should not catch exceptions. It should be left to user to swallow it or rethrow it. Now with your changes user doesn't have that control.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by gauravgopi123 <gi...@git.apache.org>.
Github user gauravgopi123 commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45100675
  
    --- 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);
    --- End diff --
    
    same..


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by ilooner <gi...@git.apache.org>.
Github user ilooner commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45116535
  
    --- 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) {
    --- End diff --
    
    Done


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...

Posted by gauravgopi123 <gi...@git.apache.org>.
Github user gauravgopi123 commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45104834
  
    --- Diff: library/src/main/java/com/datatorrent/lib/appdata/gpo/GPOUtils.java ---
    @@ -123,6 +123,54 @@ public static GPOMutable deserialize(FieldsDescriptor fieldsDescriptor,
       }
     
       /**
    +   * This method deserializes the fields in the given {@link FieldsDescriptor} into a map.
    +   * @param fieldsDescriptor The {@link FieldsDescriptor} to fetch fields from.
    +   * @param dpou The {@link JSONObject} which contains the fields whose values need to be fetched.
    +   * @return A {@link Map} whose keys are field names, and whose values are possible values for those fields.
    +   */
    +  public static Map<String, Set<Object>> deserializeToMap(FieldsDescriptor fieldsDescriptor,
    +                                                          JSONObject dpou)
    +  {
    +    Map<String, Set<Object>> keyToValues = Maps.newHashMap();
    +
    +    for (String key : fieldsDescriptor.getFields().getFields()) {
    +      if (!dpou.has(key)) {
    +        throw new IllegalArgumentException("The given key " + key + " is not contained in the given JSON");
    +      }
    +
    +      Type type = fieldsDescriptor.getType(key);
    +      Set<Object> keyValues = Sets.newHashSet();
    +      keyToValues.put(key, keyValues);
    +
    +      Object keyValue;
    +
    +      try {
    +        keyValue = dpou.get(key);
    +      } catch (JSONException ex) {
    --- End diff --
    
    why catch exception? Let the user decide what to do with it


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---