Class JsonUtil


  • public final class JsonUtil
    extends java.lang.Object
    Utility class for JSON serialization and deserialization operations. Not for public use.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  JsonUtil.CustomPrettyPrinter
      This class is used to define a custom separator and array indent to achieve the same serialization in Java and .NET.
      private static class  JsonUtil.MinimalPrinter
      This class is used to define a printer which serialize to minimal string, without extra spaces and line breaks.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static org.slf4j.Logger LOGGER  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private JsonUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean areTwoJsonObjectEquals​(java.lang.String expectedString, java.lang.String toCompare)
      Compares two json strings without considering the order of the elements.
      private static com.fasterxml.jackson.databind.ObjectWriter createAndConfigureObjectWriter​(com.fasterxml.jackson.core.util.DefaultPrettyPrinter prettyPrinter)
      Creates and configure object writer with given DefaultPrettyPrinter.
      static <T> T deserializeFromStream​(java.io.InputStream content, com.fasterxml.jackson.core.type.TypeReference<T> objectType)
      Deserializes passed JSON stream to object with passed type.
      static <T> T deserializeFromStream​(java.io.InputStream content, com.fasterxml.jackson.databind.JavaType objectType)
      Deserializes passed JSON stream to object with passed type.
      static <T> T deserializeFromStream​(java.io.InputStream content, java.lang.Class<T> objectType)
      Deserializes passed JSON stream to object with passed type.
      static <T> T deserializeFromString​(java.lang.String content, com.fasterxml.jackson.core.type.TypeReference<T> objectType)
      Deserializes passed JSON string to object with passed type.
      static <T> T deserializeFromString​(java.lang.String content, com.fasterxml.jackson.databind.JavaType objectType)
      Deserializes passed JSON string to object with passed type.
      static <T> T deserializeFromString​(java.lang.String content, java.lang.Class<T> objectType)
      Deserializes passed JSON string to object with passed type.
      static void serializeToMinimalStream​(java.io.OutputStream outputStream, java.lang.Object value)
      Serializes passed object to minimal JSON without spaces and line breaks and writes it into provided stream.
      static java.lang.String serializeToMinimalString​(java.lang.Object value)
      Serializes passed object to minimal JSON string without spaces and line breaks.
      static void serializeToStream​(java.io.OutputStream outputStream, java.lang.Object value)
      Serializes passed object to provided JSON output stream.
      private static void serializeToStream​(java.io.OutputStream outputStream, java.lang.Object value, com.fasterxml.jackson.core.util.DefaultPrettyPrinter prettyPrinter)
      Serializes passed object to provided JSON output stream.
      static java.lang.String serializeToString​(java.lang.Object value)
      Serializes passed object to JSON string.
      private static java.lang.String serializeToString​(java.lang.Object value, com.fasterxml.jackson.core.util.DefaultPrettyPrinter prettyPrinter)
      Serializes passed object to JSON string.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOGGER

        private static final org.slf4j.Logger LOGGER
    • Constructor Detail

      • JsonUtil

        private JsonUtil()
    • Method Detail

      • areTwoJsonObjectEquals

        public static boolean areTwoJsonObjectEquals​(java.lang.String expectedString,
                                                     java.lang.String toCompare)
                                              throws java.io.IOException
        Compares two json strings without considering the order of the elements.
        Parameters:
        expectedString - expected json string
        toCompare - string for comparison
        Returns:
        true if two json string are equals, false otherwise
        Throws:
        java.io.IOException - if an I/O error occurs
      • serializeToStream

        public static void serializeToStream​(java.io.OutputStream outputStream,
                                             java.lang.Object value)
        Serializes passed object to provided JSON output stream.
        Parameters:
        outputStream - stream to which the object will be serialized
        value - the object which will be serialized
      • serializeToString

        public static java.lang.String serializeToString​(java.lang.Object value)
        Serializes passed object to JSON string.
        Parameters:
        value - the object which will be serialized
        Returns:
        the JSON string representation of passed object or null if it is impossible to serialize to JSON
      • serializeToMinimalStream

        public static void serializeToMinimalStream​(java.io.OutputStream outputStream,
                                                    java.lang.Object value)
        Serializes passed object to minimal JSON without spaces and line breaks and writes it into provided stream.
        Parameters:
        outputStream - stream to which the object will be serialized
        value - the object which will be serialized
      • serializeToMinimalString

        public static java.lang.String serializeToMinimalString​(java.lang.Object value)
        Serializes passed object to minimal JSON string without spaces and line breaks.
        Parameters:
        value - the object which will be serialized
        Returns:
        the minimal JSON string representation of passed object or null if it is impossible to serialize to JSON
      • deserializeFromStream

        public static <T> T deserializeFromStream​(java.io.InputStream content,
                                                  java.lang.Class<T> objectType)
        Deserializes passed JSON stream to object with passed type.
        Type Parameters:
        T - the type of object which will be deserialized
        Parameters:
        content - the JSON stream which represent object
        objectType - the class of object as Class which will be deserialized
        Returns:
        the deserialized object or null if operation of deserialization is impossible
      • deserializeFromStream

        public static <T> T deserializeFromStream​(java.io.InputStream content,
                                                  com.fasterxml.jackson.core.type.TypeReference<T> objectType)
        Deserializes passed JSON stream to object with passed type.
        Type Parameters:
        T - the type of object which will be deserialized
        Parameters:
        content - the JSON stream which represent object
        objectType - the class of object as TypeReference which will be deserialized
        Returns:
        the deserialized object or null if operation of deserialization is impossible
      • deserializeFromStream

        public static <T> T deserializeFromStream​(java.io.InputStream content,
                                                  com.fasterxml.jackson.databind.JavaType objectType)
        Deserializes passed JSON stream to object with passed type.
        Type Parameters:
        T - the type of object which will be deserialized
        Parameters:
        content - the JSON stream which represent object
        objectType - the class of object as JavaType which will be deserialized
        Returns:
        the deserialized object or null if operation of deserialization is impossible
      • deserializeFromString

        public static <T> T deserializeFromString​(java.lang.String content,
                                                  java.lang.Class<T> objectType)
        Deserializes passed JSON string to object with passed type.
        Type Parameters:
        T - the type of object which will be deserialized
        Parameters:
        content - the JSON string which represent object
        objectType - the class of object as Class which will be deserialized
        Returns:
        the deserialized object or null if operation of deserialization is impossible
      • deserializeFromString

        public static <T> T deserializeFromString​(java.lang.String content,
                                                  com.fasterxml.jackson.core.type.TypeReference<T> objectType)
        Deserializes passed JSON string to object with passed type.
        Type Parameters:
        T - the type of object which will be deserialized
        Parameters:
        content - the JSON string which represent object
        objectType - the class of object as TypeReference which will be deserialized
        Returns:
        the deserialized object or null if operation of deserialization is impossible
      • deserializeFromString

        public static <T> T deserializeFromString​(java.lang.String content,
                                                  com.fasterxml.jackson.databind.JavaType objectType)
        Deserializes passed JSON string to object with passed type.
        Type Parameters:
        T - the type of object which will be deserialized
        Parameters:
        content - the JSON string which represent object
        objectType - the class of object as JavaType which will be deserialized
        Returns:
        the deserialized object or null if operation of deserialization is impossible
      • createAndConfigureObjectWriter

        private static com.fasterxml.jackson.databind.ObjectWriter createAndConfigureObjectWriter​(com.fasterxml.jackson.core.util.DefaultPrettyPrinter prettyPrinter)
        Creates and configure object writer with given DefaultPrettyPrinter.
        Parameters:
        prettyPrinter - specified pretty printer for indentation
        Returns:
        configured object writer
      • serializeToStream

        private static void serializeToStream​(java.io.OutputStream outputStream,
                                              java.lang.Object value,
                                              com.fasterxml.jackson.core.util.DefaultPrettyPrinter prettyPrinter)
        Serializes passed object to provided JSON output stream.
        Parameters:
        outputStream - stream to which the object will be serialized
        value - the object which will be serialized
        prettyPrinter - specified pretty printer for indentation
      • serializeToString

        private static java.lang.String serializeToString​(java.lang.Object value,
                                                          com.fasterxml.jackson.core.util.DefaultPrettyPrinter prettyPrinter)
        Serializes passed object to JSON string.
        Parameters:
        value - the object which will be serialized
        prettyPrinter - specified pretty printer for indentation
        Returns:
        the JSON string representation of passed object or null if it is impossible to serialize to JSON