Class JsonFactory

  • Direct Known Subclasses:
    GsonFactory, JacksonFactory, MockJsonFactory

    public abstract class JsonFactory
    extends java.lang.Object
    Abstract low-level JSON factory.

    Implementation is thread-safe, and sub-classes must be thread-safe. For maximum efficiency, applications should use a single globally-shared instance of the JSON factory.

    Since:
    1.3
    • Constructor Detail

      • JsonFactory

        public JsonFactory()
    • Method Detail

      • createJsonParser

        public abstract JsonParser createJsonParser​(java.io.InputStream in)
                                             throws java.io.IOException
        Returns a new instance of a low-level JSON parser for the given input stream. The parser tries to detect the charset of the input stream by itself.
        Parameters:
        in - input stream
        Returns:
        new instance of a low-level JSON parser
        Throws:
        java.io.IOException
      • createJsonParser

        public abstract JsonParser createJsonParser​(java.io.InputStream in,
                                                    java.nio.charset.Charset charset)
                                             throws java.io.IOException
        Returns a new instance of a low-level JSON parser for the given input stream.
        Parameters:
        in - input stream
        charset - charset in which the input stream is encoded or null to let the parser detect the charset
        Returns:
        new instance of a low-level JSON parser
        Throws:
        java.io.IOException
        Since:
        1.10
      • createJsonParser

        public abstract JsonParser createJsonParser​(java.lang.String value)
                                             throws java.io.IOException
        Returns a new instance of a low-level JSON parser for the given string value.
        Parameters:
        value - string value
        Returns:
        new instance of a low-level JSON parser
        Throws:
        java.io.IOException
      • createJsonParser

        public abstract JsonParser createJsonParser​(java.io.Reader reader)
                                             throws java.io.IOException
        Returns a new instance of a low-level JSON parser for the given reader.
        Parameters:
        reader - reader
        Returns:
        new instance of a low-level JSON parser
        Throws:
        java.io.IOException
      • createJsonGenerator

        public abstract JsonGenerator createJsonGenerator​(java.io.OutputStream out,
                                                          java.nio.charset.Charset enc)
                                                   throws java.io.IOException
        Returns a new instance of a low-level JSON serializer for the given output stream and encoding.
        Parameters:
        out - output stream
        enc - encoding
        Returns:
        new instance of a low-level JSON serializer
        Throws:
        java.io.IOException
        Since:
        1.10
      • createJsonGenerator

        public abstract JsonGenerator createJsonGenerator​(java.io.Writer writer)
                                                   throws java.io.IOException
        Returns a new instance of a low-level JSON serializer for the given writer.
        Parameters:
        writer - writer
        Returns:
        new instance of a low-level JSON serializer
        Throws:
        java.io.IOException
      • createJsonObjectParser

        public final JsonObjectParser createJsonObjectParser()
        Creates an object parser which uses this factory to parse JSON data.
        Since:
        1.10
      • toString

        public final java.lang.String toString​(java.lang.Object item)
                                        throws java.io.IOException
        Returns a serialized JSON string representation for the given item using JsonGenerator.serialize(Object).
        Parameters:
        item - data key/value pairs
        Returns:
        serialized JSON string representation
        Throws:
        java.io.IOException
      • toPrettyString

        public final java.lang.String toPrettyString​(java.lang.Object item)
                                              throws java.io.IOException
        Returns a pretty-printed serialized JSON string representation for the given item using JsonGenerator.serialize(Object) with JsonGenerator.enablePrettyPrint().

        The specifics of how the JSON representation is made pretty is implementation dependent, and should not be relied on. However, it is assumed to be legal, and in fact differs from toString(Object) only by adding whitespace that does not change its meaning.

        Parameters:
        item - data key/value pairs
        Returns:
        serialized JSON string representation
        Throws:
        java.io.IOException
        Since:
        1.6
      • toByteArray

        public final byte[] toByteArray​(java.lang.Object item)
                                 throws java.io.IOException
        Returns a UTF-8 encoded byte array of the serialized JSON representation for the given item using JsonGenerator.serialize(Object).
        Parameters:
        item - data key/value pairs
        Returns:
        byte array of the serialized JSON representation
        Throws:
        java.io.IOException
        Since:
        1.7
      • toString

        private java.lang.String toString​(java.lang.Object item,
                                          boolean pretty)
                                   throws java.io.IOException
        Returns a serialized JSON string representation for the given item using JsonGenerator.serialize(Object).
        Parameters:
        item - data key/value pairs
        pretty - whether to return a pretty representation
        Returns:
        serialized JSON string representation
        Throws:
        java.io.IOException
      • toByteStream

        private java.io.ByteArrayOutputStream toByteStream​(java.lang.Object item,
                                                           boolean pretty)
                                                    throws java.io.IOException
        Returns a UTF-8 byte array output stream of the serialized JSON representation for the given item using JsonGenerator.serialize(Object).
        Parameters:
        item - data key/value pairs
        pretty - whether to return a pretty representation
        Returns:
        serialized JSON string representation
        Throws:
        java.io.IOException
      • fromString

        public final <T> T fromString​(java.lang.String value,
                                      java.lang.Class<T> destinationClass)
                               throws java.io.IOException
        Parses a string value as a JSON object, array, or value into a new instance of the given destination class using JsonParser.parse(Class).
        Parameters:
        value - JSON string value
        destinationClass - destination class that has an accessible default constructor to use to create a new instance
        Returns:
        new instance of the parsed destination class
        Throws:
        java.io.IOException
        Since:
        1.4
      • fromInputStream

        public final <T> T fromInputStream​(java.io.InputStream inputStream,
                                           java.lang.Class<T> destinationClass)
                                    throws java.io.IOException
        Parse and close an input stream as a JSON object, array, or value into a new instance of the given destination class using JsonParser.parseAndClose(Class).

        Tries to detect the charset of the input stream automatically.

        Parameters:
        inputStream - JSON value in an input stream
        destinationClass - destination class that has an accessible default constructor to use to create a new instance
        Returns:
        new instance of the parsed destination class
        Throws:
        java.io.IOException
        Since:
        1.7
      • fromInputStream

        public final <T> T fromInputStream​(java.io.InputStream inputStream,
                                           java.nio.charset.Charset charset,
                                           java.lang.Class<T> destinationClass)
                                    throws java.io.IOException
        Parse and close an input stream as a JSON object, array, or value into a new instance of the given destination class using JsonParser.parseAndClose(Class).
        Parameters:
        inputStream - JSON value in an input stream
        charset - Charset in which the stream is encoded
        destinationClass - destination class that has an accessible default constructor to use to create a new instance
        Returns:
        new instance of the parsed destination class
        Throws:
        java.io.IOException
        Since:
        1.10
      • fromReader

        public final <T> T fromReader​(java.io.Reader reader,
                                      java.lang.Class<T> destinationClass)
                               throws java.io.IOException
        Parse and close a reader as a JSON object, array, or value into a new instance of the given destination class using JsonParser.parseAndClose(Class).
        Parameters:
        reader - JSON value in a reader
        destinationClass - destination class that has an accessible default constructor to use to create a new instance
        Returns:
        new instance of the parsed destination class
        Throws:
        java.io.IOException
        Since:
        1.7