Class JsonUtils


  • public class JsonUtils
    extends Object
    This class contains methods used for operations on JSON type data
    Since:
    1.0.0
    Author:
    Sidhant Agarwal
    • Method Detail

      • javaObjectToJsonString

        public static String javaObjectToJsonString​(Object className)
                                             throws JsonProcessingException
        This function converts the java object and returns a JSON String
        Parameters:
        className - object of the class to be converted to JSON File
        Returns:
        generated JSON String
        Throws:
        JsonProcessingException - when JSON is not properly processed
      • jsonStringToJavaObject

        public static Object jsonStringToJavaObject​(Class<?> className,
                                                    String jsonString)
                                             throws JsonParseException,
                                                    JsonMappingException,
                                                    IOException
        This method converts the JSON String input and maps it to the java object
        Parameters:
        className - class name to which the JSON String is to be mapped
        jsonString - input JSON String(always in double quotes) (eg."{color=Black, type=FIAT}")
        Returns:
        class object with the JSON string parsed to object
        Throws:
        JsonParseException - when JSON is not properly parsed
        JsonMappingException - when JSON is not properly mapped
        IOException - when location is not found
      • jsonToJacksonJson

        public static String jsonToJacksonJson​(String jsonString,
                                               String key)
                                        throws IOException
        This function returns the value associated with the label of the input JSON
        Parameters:
        jsonString - input JSON String(always in double quotes) (eg."{color=Black, type=FIAT}")
        key - label of the JSON String whose value is to be retrieved
        Returns:
        value of the corresponding key input
        Throws:
        IOException - when file is not found
      • jsonStringToJavaList

        public static List<Object> jsonStringToJavaList​(String jsonArray)
                                                 throws JsonParseException,
                                                        JsonMappingException,
                                                        IOException
        This method converts a JSON String containing multiple JSON and stores them in a java list
        Parameters:
        jsonArray - input String containing array of JSON string(always in double quotes) (eg."[{color=Black, type=BMW}, {color=Red, type=FIAT}]")
        Returns:
        list of JSON string
        Throws:
        JsonParseException - when JSON is not properly parsed
        JsonMappingException - when JSON is not properly mapped
        IOException - when file is not found
      • jsonStringToJavaMap

        public static Map<String,​Object> jsonStringToJavaMap​(String jsonString)
                                                            throws JsonParseException,
                                                                   JsonMappingException,
                                                                   IOException
        This method converts a JSON String containing multiple JSON and stores them in a java Map
        Parameters:
        jsonString - input String containing array of JSON string(always in double quotes) (eg."[{color=Black, type=BMW}, {color=Red, type=FIAT}]")
        Returns:
        java map containing JSON inputs
        Throws:
        JsonParseException - when JSON is not properly parsed
        JsonMappingException - when JSON is not properly mapped
        IOException - when file is not found