Package com.google.api.client.json
Class JsonFactory
java.lang.Object
com.google.api.client.json.JsonFactory
- Direct Known Subclasses:
GsonFactory
,JacksonFactory
,MockJsonFactory
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract JsonGenerator
createJsonGenerator
(OutputStream out, Charset enc) Returns a new instance of a low-level JSON serializer for the given output stream and encoding.abstract JsonGenerator
createJsonGenerator
(Writer writer) Returns a new instance of a low-level JSON serializer for the given writer.final JsonObjectParser
Creates an object parser which uses this factory to parse JSON data.abstract JsonParser
Returns a new instance of a low-level JSON parser for the given input stream.abstract JsonParser
createJsonParser
(InputStream in, Charset charset) Returns a new instance of a low-level JSON parser for the given input stream.abstract JsonParser
createJsonParser
(Reader reader) Returns a new instance of a low-level JSON parser for the given reader.abstract JsonParser
createJsonParser
(String value) Returns a new instance of a low-level JSON parser for the given string value.final <T> T
fromInputStream
(InputStream inputStream, Class<T> destinationClass) Parse and close an input stream as a JSON object, array, or value into a new instance of the given destination class usingJsonParser.parseAndClose(Class)
.final <T> T
fromInputStream
(InputStream inputStream, Charset charset, Class<T> destinationClass) Parse and close an input stream as a JSON object, array, or value into a new instance of the given destination class usingJsonParser.parseAndClose(Class)
.final <T> T
fromReader
(Reader reader, Class<T> destinationClass) Parse and close a reader as a JSON object, array, or value into a new instance of the given destination class usingJsonParser.parseAndClose(Class)
.final <T> T
fromString
(String value, Class<T> destinationClass) Parses a string value as a JSON object, array, or value into a new instance of the given destination class usingJsonParser.parse(Class)
.final byte[]
toByteArray
(Object item) Returns a UTF-8 encoded byte array of the serialized JSON representation for the given item usingJsonGenerator.serialize(Object)
.private ByteArrayOutputStream
toByteStream
(Object item, boolean pretty) Returns a UTF-8 byte array output stream of the serialized JSON representation for the given item usingJsonGenerator.serialize(Object)
.final String
toPrettyString
(Object item) Returns a pretty-printed serialized JSON string representation for the given item usingJsonGenerator.serialize(Object)
withJsonGenerator.enablePrettyPrint()
.final String
Returns a serialized JSON string representation for the given item usingJsonGenerator.serialize(Object)
.private String
Returns a serialized JSON string representation for the given item usingJsonGenerator.serialize(Object)
.
-
Constructor Details
-
JsonFactory
public JsonFactory()
-
-
Method Details
-
createJsonParser
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:
IOException
-
createJsonParser
Returns a new instance of a low-level JSON parser for the given input stream.- Parameters:
in
- input streamcharset
- charset in which the input stream is encoded ornull
to let the parser detect the charset- Returns:
- new instance of a low-level JSON parser
- Throws:
IOException
- Since:
- 1.10
-
createJsonParser
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:
IOException
-
createJsonParser
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:
IOException
-
createJsonGenerator
Returns a new instance of a low-level JSON serializer for the given output stream and encoding.- Parameters:
out
- output streamenc
- encoding- Returns:
- new instance of a low-level JSON serializer
- Throws:
IOException
- Since:
- 1.10
-
createJsonGenerator
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:
IOException
-
createJsonObjectParser
Creates an object parser which uses this factory to parse JSON data.- Since:
- 1.10
-
toString
Returns a serialized JSON string representation for the given item usingJsonGenerator.serialize(Object)
.- Parameters:
item
- data key/value pairs- Returns:
- serialized JSON string representation
- Throws:
IOException
-
toPrettyString
Returns a pretty-printed serialized JSON string representation for the given item usingJsonGenerator.serialize(Object)
withJsonGenerator.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:
IOException
- Since:
- 1.6
-
toByteArray
Returns a UTF-8 encoded byte array of the serialized JSON representation for the given item usingJsonGenerator.serialize(Object)
.- Parameters:
item
- data key/value pairs- Returns:
- byte array of the serialized JSON representation
- Throws:
IOException
- Since:
- 1.7
-
toString
Returns a serialized JSON string representation for the given item usingJsonGenerator.serialize(Object)
.- Parameters:
item
- data key/value pairspretty
- whether to return a pretty representation- Returns:
- serialized JSON string representation
- Throws:
IOException
-
toByteStream
Returns a UTF-8 byte array output stream of the serialized JSON representation for the given item usingJsonGenerator.serialize(Object)
.- Parameters:
item
- data key/value pairspretty
- whether to return a pretty representation- Returns:
- serialized JSON string representation
- Throws:
IOException
-
fromString
Parses a string value as a JSON object, array, or value into a new instance of the given destination class usingJsonParser.parse(Class)
.- Parameters:
value
- JSON string valuedestinationClass
- destination class that has an accessible default constructor to use to create a new instance- Returns:
- new instance of the parsed destination class
- Throws:
IOException
- Since:
- 1.4
-
fromInputStream
public final <T> T fromInputStream(InputStream inputStream, Class<T> destinationClass) throws IOException Parse and close an input stream as a JSON object, array, or value into a new instance of the given destination class usingJsonParser.parseAndClose(Class)
.Tries to detect the charset of the input stream automatically.
- Parameters:
inputStream
- JSON value in an input streamdestinationClass
- destination class that has an accessible default constructor to use to create a new instance- Returns:
- new instance of the parsed destination class
- Throws:
IOException
- Since:
- 1.7
-
fromInputStream
public final <T> T fromInputStream(InputStream inputStream, Charset charset, Class<T> destinationClass) throws IOException Parse and close an input stream as a JSON object, array, or value into a new instance of the given destination class usingJsonParser.parseAndClose(Class)
.- Parameters:
inputStream
- JSON value in an input streamcharset
- Charset in which the stream is encodeddestinationClass
- destination class that has an accessible default constructor to use to create a new instance- Returns:
- new instance of the parsed destination class
- Throws:
IOException
- Since:
- 1.10
-
fromReader
Parse and close a reader as a JSON object, array, or value into a new instance of the given destination class usingJsonParser.parseAndClose(Class)
.- Parameters:
reader
- JSON value in a readerdestinationClass
- destination class that has an accessible default constructor to use to create a new instance- Returns:
- new instance of the parsed destination class
- Throws:
IOException
- Since:
- 1.7
-