Class JSONUtils

java.lang.Object
com.agenarisk.api.util.JSONUtils

public class JSONUtils extends Object
Provides convenience functionality for working with JSONObject and JSONArray classes.
Author:
Eugene Dementiev
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    convertTo2DArray(org.json.JSONObject grandParent, String parentKey, String elementKey)
    Converts an array of mixed objects into a 2D JSONArray.
    static void
    convertToJSONArray(org.json.JSONObject grandParent, String parentKey, String childKey)
    Omits the key `childKey` and puts its contents directly under the `parentKey` as a JSONArray.
    static String
    createMissingAttrMessage(org.json.JSONException ex)
    This is a helper function to create a message for a JSONException that is formatted as suitable for display to a user.
    static boolean
    equalsIgnoreCase(org.json.JSONArray ja1, org.json.JSONArray ja2)
    Checks for deep equality of two JSONArrays, resolving elements that are JSONObjects or JSONArrays recursively.
    static boolean
    equalsIgnoreCase(org.json.JSONObject jo1, org.json.JSONObject jo2)
    Checks for deep equality of two JSONObjects, resolving elements that are JSONObjects or JSONArrays recursively.
    static org.json.JSONArray
    getNNJArray(org.json.JSONArray parent, int index)
     
    static org.json.JSONArray
    getNNJArray(org.json.JSONObject parent, Object key)
     
    static org.json.JSONObject
    getnnJObject(org.json.JSONArray parent, int index)
     
    static org.json.JSONObject
    getNNJObject(org.json.JSONObject parent, Object key)
     
    static org.json.JSONObject
    toJSONObject(Object[][] array)
    Converts a 2D Object array into a JSONObject.
    static <T> List<T>
    toList(org.json.JSONArray jarray, Class<T> classType)
    Converts JSONArray to a List of provided type.
    static void
    Applies the provided action to the provided object.
    static void
    wrapArrayElements(org.json.JSONArray parent, String wrapperName)
    Wraps all elements of the provided JSONArray into JSONObjects that each contains the element under the provided key.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JSONUtils

      public JSONUtils()
  • Method Details

    • toJSONObject

      public static org.json.JSONObject toJSONObject(Object[][] array)
      Converts a 2D Object array into a JSONObject.
      The first dimension represents a list of map entries.
      The second dimension represents a map entry, where first element is its String key, and the second is the value.
      Parameters:
      array - Basic 2D Object array representation of the map
      Returns:
      JSONObject representation of the given array
    • createMissingAttrMessage

      public static String createMissingAttrMessage(org.json.JSONException ex)
      This is a helper function to create a message for a JSONException that is formatted as suitable for display to a user.
      Parameters:
      ex - Exception that has the message to convert
      Returns:
      user-friendly error message
    • toList

      public static <T> List<T> toList(org.json.JSONArray jarray, Class<T> classType)
      Converts JSONArray to a List of provided type.
      Type Parameters:
      T - type of objects expected to be in the list
      Parameters:
      jarray - JSONArray array
      classType - Class of items to be in the list
      Returns:
      List containing items from the JSONArray
    • convertTo2DArray

      public static void convertTo2DArray(org.json.JSONObject grandParent, String parentKey, String elementKey) throws org.json.JSONException
      Converts an array of mixed objects into a 2D JSONArray.
      The array resides under the `parentKey` in grandParent JSONObject.
      Elements of the original array are JSONObjects and contain an item of key `elementKey`. This item could in turn be a JSONArray or a simple type.
      After the operation, the original JSONArray will contain JSONArrays, such that the `elementKey` level is omitted.
      For example,
      {"table":{"probs":[{"cell":0.2},{"cell":0.8}]}}
      will be converted to
      {"table":{"probs":[[0.2],[0.8]]}}
      And
      {"table":{"probs":[{"cell":[0.2,0.7},{"cell":[0.8,0.3]}]}}
      will be converted to
      {"table":{"probs":[[0.2, 0.7],[0.8, 0.3]]}}
      Parameters:
      grandParent - JSONObject that should contain the 2D array (in the example - object `table`)
      parentKey - the key of the 2D array in grandParent (in the example - "probs")
      elementKey - the elementKey to be omitted (in the example - "cell")
      Throws:
      org.json.JSONException - on failure
    • convertToJSONArray

      public static void convertToJSONArray(org.json.JSONObject grandParent, String parentKey, String childKey) throws org.json.JSONException
      Omits the key `childKey` and puts its contents directly under the `parentKey` as a JSONArray.
      Wraps into JSONArray as necessary.
      For example,
      {"network":{"nodes":{"node":"X"}}}
      will be converted to
      {"network":{"nodes":["X"]}
      And
      {"network":{"nodes":{"node":["X","Y"]}}}
      will be converted to
      {"network":{"nodes":["X", "Y"]}
      Parameters:
      grandParent - JSONObject containing `parentKey`, in the example - object network
      parentKey - - the key under which to put the resulting array, in the example - "nodes"
      childKey - - the key to omit (and wrap contents into array if necessary), in the example - "node"
      Throws:
      org.json.JSONException - on failure
    • wrapArrayElements

      public static void wrapArrayElements(org.json.JSONArray parent, String wrapperName)
      Wraps all elements of the provided JSONArray into JSONObjects that each contains the element under the provided key.
      Parameters:
      parent - the array to wrap elements of
      wrapperName - the key name under which to wrap each element
    • equalsIgnoreCase

      public static boolean equalsIgnoreCase(org.json.JSONObject jo1, org.json.JSONObject jo2)
      Checks for deep equality of two JSONObjects, resolving elements that are JSONObjects or JSONArrays recursively.
      When the element is a string, equality is computed ignoring case.
      If an element is a String value of a number, the ".0" will be stripped from the end before comparison.
      Parameters:
      jo1 - a JSONObject
      jo2 - a second JSONObject to compare to jo1
      Returns:
      true if all leaf objects are equal (case insensitive for Strings), false otherwise
      See Also:
    • equalsIgnoreCase

      public static boolean equalsIgnoreCase(org.json.JSONArray ja1, org.json.JSONArray ja2)
      Checks for deep equality of two JSONArrays, resolving elements that are JSONObjects or JSONArrays recursively.
      When the element is a string, equality is computed ignoring case.
      If an element is a String value of a number, the ".0" will be stripped from the end before comparison.
      Parameters:
      ja1 - a JSONArray
      ja2 - another JSONArray to compare with
      Returns:
      true if all leaf objects are equal (case insensitive for Strings), false otherwise
      See Also:
    • getnnJObject

      public static org.json.JSONObject getnnJObject(org.json.JSONArray parent, int index)
    • getNNJObject

      public static org.json.JSONObject getNNJObject(org.json.JSONObject parent, Object key)
    • getNNJArray

      public static org.json.JSONArray getNNJArray(org.json.JSONObject parent, Object key)
    • getNNJArray

      public static org.json.JSONArray getNNJArray(org.json.JSONArray parent, int index)
    • traverse

      public static void traverse(Object o, Consumer<Object> action)
      Applies the provided action to the provided object. Then, if the object is JSONObject or JSONArray, recursively traverses through their elements.
      Order of traversal is the one backed by the object.
      Nulls are not handled in any way, so the action must handle them.
      Parameters:
      o - object to apply action to and potentially traverse
      action - action to apply