Class JSON


  • public class JSON
    extends java.lang.Object
    JSON util class
    Since:
    6.1.0
    Author:
    Lijun Liao (xipki)
    • Constructor Summary

      Constructors 
      Constructor Description
      JSON()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static ConfPairs deserializerConfPairs​(com.fasterxml.jackson.core.JsonParser jsonParser)  
      static com.fasterxml.jackson.databind.ObjectMapper newDefaultObjectMapper()  
      static <T> T parseConf​(byte[] json, java.lang.Class<T> classOfT)  
      static <T> T parseConf​(java.io.File jsonFile, java.lang.Class<T> classOfT)  
      static <T> T parseConf​(java.io.InputStream jsonInputStream, java.lang.Class<T> classOfT)  
      static <T> T parseConf​(java.lang.String json, java.lang.Class<T> classOfT)  
      static <T> T parseConf​(java.nio.file.Path jsonFilePath, java.lang.Class<T> classOfT)  
      static <T> T parseObject​(byte[] json, java.lang.Class<T> classOfT)  
      static <T> T parseObject​(java.io.File jsonFile, java.lang.Class<T> classOfT)  
      static <T> T parseObject​(java.io.InputStream jsonInputStream, java.lang.Class<T> classOfT)
      Deserialize the object from the input stream.
      static <T> T parseObject​(java.lang.String json, java.lang.Class<T> classOfT)  
      static <T> T parseObject​(java.nio.file.Path jsonFilePath, java.lang.Class<T> classOfT)  
      static <T> T parseObjectAndClose​(java.io.InputStream jsonInputStream, java.lang.Class<T> classOfT)
      Deserialize the object from the input stream and closes the inputstream.
      static java.lang.String toJson​(java.lang.Object obj)  
      static byte[] toJSONBytes​(java.lang.Object obj)  
      static java.lang.String toPrettyJson​(java.lang.Object obj)  
      static void writeJSON​(java.lang.Object object, java.io.OutputStream outputStream)
      Serialize the object to the output stream.
      static void writeJSONAndClose​(java.lang.Object object, java.io.OutputStream outputStream)
      Serialize the object to the output stream.
      static void writePrettyJSON​(java.lang.Object object, java.io.OutputStream outputStream)
      Serialize the object in pretty format to the output stream.
      static void writePrettyJSONAndClose​(java.lang.Object object, java.io.OutputStream outputStream)
      Serialize the object in pretty format to the output stream.
      • Methods inherited from class java.lang.Object

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

      • JSON

        public JSON()
    • Method Detail

      • newDefaultObjectMapper

        public static com.fasterxml.jackson.databind.ObjectMapper newDefaultObjectMapper()
      • parseObject

        public static <T> T parseObject​(java.lang.String json,
                                        java.lang.Class<T> classOfT)
      • parseObject

        public static <T> T parseObject​(byte[] json,
                                        java.lang.Class<T> classOfT)
      • parseObject

        public static <T> T parseObject​(java.nio.file.Path jsonFilePath,
                                        java.lang.Class<T> classOfT)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • parseObject

        public static <T> T parseObject​(java.io.File jsonFile,
                                        java.lang.Class<T> classOfT)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • parseConf

        public static <T> T parseConf​(byte[] json,
                                      java.lang.Class<T> classOfT)
      • parseConf

        public static <T> T parseConf​(java.lang.String json,
                                      java.lang.Class<T> classOfT)
      • parseConf

        public static <T> T parseConf​(java.io.File jsonFile,
                                      java.lang.Class<T> classOfT)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • parseConf

        public static <T> T parseConf​(java.nio.file.Path jsonFilePath,
                                      java.lang.Class<T> classOfT)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • parseConf

        public static <T> T parseConf​(java.io.InputStream jsonInputStream,
                                      java.lang.Class<T> classOfT)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • parseObject

        public static <T> T parseObject​(java.io.InputStream jsonInputStream,
                                        java.lang.Class<T> classOfT)
                                 throws java.io.IOException
        Deserialize the object from the input stream. The specified stream remains open after this method returns.
        Type Parameters:
        T - the object type of serialized object.
        Parameters:
        jsonInputStream - the input stream containing the serialized object.
        classOfT - the class of deserialized object.
        Returns:
        the serialized object
        Throws:
        java.io.IOException - if IO error occurs while reading the stream.
      • parseObjectAndClose

        public static <T> T parseObjectAndClose​(java.io.InputStream jsonInputStream,
                                                java.lang.Class<T> classOfT)
                                         throws java.io.IOException
        Deserialize the object from the input stream and closes the inputstream. The specified stream is closed after this method returns.
        Type Parameters:
        T - the object type of serialized object.
        Parameters:
        jsonInputStream - the input stream containing the serialized object.
        classOfT - the class of deserialized object.
        Returns:
        the serialized object
        Throws:
        java.io.IOException - if IO error occurs while reading the stream.
      • toJson

        public static java.lang.String toJson​(java.lang.Object obj)
      • toJSONBytes

        public static byte[] toJSONBytes​(java.lang.Object obj)
      • toPrettyJson

        public static java.lang.String toPrettyJson​(java.lang.Object obj)
      • writeJSON

        public static void writeJSON​(java.lang.Object object,
                                     java.io.OutputStream outputStream)
                              throws java.io.IOException
        Serialize the object to the output stream. The specified stream remains open after this method returns.
        Parameters:
        object - object to be serialized.
        outputStream - output stream to which the serialized object is written.
        Throws:
        java.io.IOException - if IO error occurs while writting to the stream.
      • writeJSONAndClose

        public static void writeJSONAndClose​(java.lang.Object object,
                                             java.io.OutputStream outputStream)
                                      throws java.io.IOException
        Serialize the object to the output stream. The specified stream is closed after this method returns.
        Parameters:
        object - object to be serialized.
        outputStream - output stream to which the serialized object is written.
        Throws:
        java.io.IOException - if IO error occurs while writting to the stream.
      • writePrettyJSON

        public static void writePrettyJSON​(java.lang.Object object,
                                           java.io.OutputStream outputStream)
                                    throws java.io.IOException
        Serialize the object in pretty format to the output stream. The specified stream remains open after this method returns.
        Parameters:
        object - object to be serialized.
        outputStream - output stream to which the serialized object is written.
        Throws:
        java.io.IOException - if IO error occurs while writting to the stream.
      • writePrettyJSONAndClose

        public static void writePrettyJSONAndClose​(java.lang.Object object,
                                                   java.io.OutputStream outputStream)
                                            throws java.io.IOException
        Serialize the object in pretty format to the output stream. The specified stream is closed after this method returns.
        Parameters:
        object - object to be serialized.
        outputStream - output stream to which the serialized object is written.
        Throws:
        java.io.IOException - if IO error occurs while writting to the stream.
      • deserializerConfPairs

        public static ConfPairs deserializerConfPairs​(com.fasterxml.jackson.core.JsonParser jsonParser)
                                               throws java.io.IOException
        Throws:
        java.io.IOException