Class JsonUtil


  • public class JsonUtil
    extends Object
    This class provides JSON utilites.
    Since:
    0.0.1
    Author:
    Pranav Kumar
    • Method Detail

      • inputStreamtoJavaObject

        public static Object inputStreamtoJavaObject​(InputStream stream,
                                                     Class<?> clazz)
                                              throws UnsupportedEncodingException
        This method converts InputStream to JavaObject.
        Parameters:
        stream - The stream that needs to be converted
        clazz - The class to which conversion is required
        Returns:
        The converted Java object
        Throws:
        UnsupportedEncodingException - the unsupported encoding exception
        com.google.gson.JsonSyntaxException - the json syntax exception
        com.google.gson.JsonIOException - the json IO exception
      • getJSONObject

        public static org.json.simple.JSONObject getJSONObject​(org.json.simple.JSONObject jsonObject,
                                                               Object key)
        This method returns the Json Object as value from identity.json object(JSONObject). jsonObject -> then identity demographic json object key -> demographic json label name EX:- demographicIdentity : { "identity" : { "fullName" : [ { "language" : "eng", "value" : "Taleev Aalam" }, { "language": "ara", "value" : "Taleev Aalam" } ] } method call :- getJSONObject(demographicIdentity,identity)
        Parameters:
        jsonObject - the json object
        key - the key
        Returns:
        the JSON object
      • getJSONArray

        public static org.json.simple.JSONArray getJSONArray​(org.json.simple.JSONObject jsonObject,
                                                             Object key)
        This method returns JSONArray from JSONObject. argument 'jsonObject' -> demographic identity json as JSONObject. argument key -> label name of demographic identity json. Ex:- "identity" : { "fullName" : [ { "language" : "eng", "value" : "Taleev Aalam" }, { "language" : "ara", "value" : "Taleev Aalam" } ] }
        Parameters:
        jsonObject - the json object
        key - the key
        Returns:
        the JSON array
      • readValue

        public static <T> T readValue​(String jsonString,
                                      Class<?> clazz)
                               throws IOException
        Object mapper read value. This method maps the jsonString to particular type
        Type Parameters:
        T - the generic type
        Parameters:
        jsonString - the json string
        clazz - the clazz
        Returns:
        the t
        Throws:
        com.fasterxml.jackson.core.JsonParseException - the json parse exception
        com.fasterxml.jackson.databind.JsonMappingException - the json mapping exception
        IOException - Signals that an I/O exception has occurred.
      • getJSONValue

        public static <T> T getJSONValue​(org.json.simple.JSONObject jsonObject,
                                         String key)
        Gets the JSON value.
        Type Parameters:
        T - the generic type
        Parameters:
        jsonObject - the json object
        key - the key
        Returns:
        the JSON value
      • getJSONObjectFromArray

        public static org.json.simple.JSONObject getJSONObjectFromArray​(org.json.simple.JSONArray jsonObject,
                                                                        int key)
        Iterates the JSONArray and returns JSONObject for given index.
        Parameters:
        jsonObject - the json object
        key - the key
        Returns:
        the JSON object
      • objectMapperReadValue

        public static <T> T objectMapperReadValue​(String jsonString,
                                                  Class<?> clazz)
                                           throws IOException
        Object mapper read value. This method maps the jsonString to particular type
        Type Parameters:
        T - the generic type
        Parameters:
        jsonString - the json string
        clazz - the clazz
        Returns:
        the t
        Throws:
        com.fasterxml.jackson.core.JsonParseException - the json parse exception
        com.fasterxml.jackson.databind.JsonMappingException - the json mapping exception
        IOException - Signals that an I/O exception has occurred.
      • getJsonValues

        public static JsonValue[] getJsonValues​(org.json.simple.JSONObject demographicIdentity,
                                                Object identityKey)
        Gets the json values. Returns JsonValue[] java array for the particular key in demographic json.
        Parameters:
        identityKey - the identity key
        Returns:
        the json values
      • mapJsonNodeToJavaObject

        public static <T> T[] mapJsonNodeToJavaObject​(Class<? extends Object> genericType,
                                                      org.json.simple.JSONArray demographicJsonNode)
        Map json node to java object.
        Type Parameters:
        T - the generic type
        Parameters:
        genericType - the generic type
        demographicJsonNode - the demographic json node
        Returns:
        the t[]