Class JsonUtils


  • public final class JsonUtils
    extends Object
    A simple class that helps serialize and deserialize JSON objects in various formats and inputs
    • Constructor Summary

      Constructors 
      Constructor Description
      JsonUtils()  
    • Method Summary

      Modifier and Type Method Description
      static <T> T fromFile​(File file, com.fasterxml.jackson.core.type.TypeReference<T> type)  
      static <T> T fromFile​(File file, Class<T> type)
      Deserialize a JSON from a File to a given class type
      static <T> T fromFile​(InputStream jsonStream, Class<T> type)
      Deserialize a JSON from a File to a given class type
      static <T> T fromFile​(String filePath, Class<T> type)
      Deserialize a JSON from a File to a given class type
      static <T> T fromString​(String jsonString, com.fasterxml.jackson.core.type.TypeReference<T> type)
      Deserialize a JSON from a String to a given class type
      static <T> T fromString​(String jsonString, Class<T> type)
      Deserialize a JSON from a String to a given class type
      static String toString​(Object object)
      Serializes a given object instance into a JSON and returns it as a String
    • Constructor Detail

      • JsonUtils

        public JsonUtils()
    • Method Detail

      • fromString

        public static <T> T fromString​(String jsonString,
                                       com.fasterxml.jackson.core.type.TypeReference<T> type)
                                throws com.fasterxml.jackson.core.JsonProcessingException
        Deserialize a JSON from a String to a given class type
        Type Parameters:
        T - generic type
        Parameters:
        jsonString - json string
        type - target class type or collection
        Returns:
        generic class for mapper
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException - if JSON cannot be processed
      • fromString

        public static <T> T fromString​(String jsonString,
                                       Class<T> type)
                                throws com.fasterxml.jackson.core.JsonProcessingException
        Deserialize a JSON from a String to a given class type
        Type Parameters:
        T - generic type
        Parameters:
        jsonString - json string
        type - target class type or collection
        Returns:
        generic class for mapper
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException - if JSON cannot be processed
      • fromFile

        public static <T> T fromFile​(File file,
                                     Class<T> type)
                              throws IOException
        Deserialize a JSON from a File to a given class type
        Type Parameters:
        T - generic type
        Parameters:
        file - json file
        type - target class type or collection
        Returns:
        generic class for mapper
        Throws:
        IOException - if file cannot be found
      • fromFile

        public static <T> T fromFile​(File file,
                                     com.fasterxml.jackson.core.type.TypeReference<T> type)
                              throws IOException
        Type Parameters:
        T - generic type
        Parameters:
        file - json file
        type - target class type of collection
        Returns:
        generic class for mapper
        Throws:
        IOException - if file cannot be found
      • fromFile

        public static <T> T fromFile​(InputStream jsonStream,
                                     Class<T> type)
                              throws IOException
        Deserialize a JSON from a File to a given class type
        Type Parameters:
        T - generic type
        Parameters:
        jsonStream - json stream
        type - target class type or collection
        Returns:
        generic class for mapper
        Throws:
        IOException - if file cannot be found
      • fromFile

        public static <T> T fromFile​(String filePath,
                                     Class<T> type)
                              throws IOException
        Deserialize a JSON from a File to a given class type
        Type Parameters:
        T - generic type
        Parameters:
        filePath - json file path
        type - target class type or collection
        Returns:
        generic class for mapper
        Throws:
        IOException - if file cannot be found
      • toString

        public static String toString​(Object object)
                               throws com.fasterxml.jackson.core.JsonProcessingException
        Serializes a given object instance into a JSON and returns it as a String
        Parameters:
        object - json object
        Returns:
        String that represents JSON
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException - if JSON cannot be parsed