Class JsonFactory (1.45.2)

public abstract class JsonFactory

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.

Inheritance

Object > JsonFactory

Constructors

JsonFactory()

public JsonFactory()

Methods

<T>fromInputStream(InputStream inputStream, Class<T> destinationClass)

public 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 using <xref uid="com.google.api.client.json.JsonParser.

Tries to detect the charset of the input stream automatically.

Parameters
Name Description
inputStream InputStream

JSON value in an input stream

destinationClass Class<T>

destination class that has an accessible default constructor to use to create a new instance

Returns
Type Description
T

new instance of the parsed destination class

Exceptions
Type Description
IOException

<T>fromInputStream(InputStream inputStream, Charset charset, Class<T> destinationClass)

public 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 using <xref uid="com.google.api.client.json.JsonParser.

Parameters
Name Description
inputStream InputStream

JSON value in an input stream

charset Charset

Charset in which the stream is encoded

destinationClass Class<T>

destination class that has an accessible default constructor to use to create a new instance

Returns
Type Description
T

new instance of the parsed destination class

Exceptions
Type Description
IOException

<T>fromReader(Reader reader, Class<T> destinationClass)

public 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 using <xref uid="com.google.api.client.json.JsonParser.

Parameters
Name Description
reader Reader

JSON value in a reader

destinationClass Class<T>

destination class that has an accessible default constructor to use to create a new instance

Returns
Type Description
T

new instance of the parsed destination class

Exceptions
Type Description
IOException

<T>fromString(String value, Class<T> destinationClass)

public 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 using <xref uid="com.google.api.client.json.JsonParser.

Parameters
Name Description
value String

JSON string value

destinationClass Class<T>

destination class that has an accessible default constructor to use to create a new instance

Returns
Type Description
T

new instance of the parsed destination class

Exceptions
Type Description
IOException

createJsonGenerator(OutputStream out, Charset enc)

public abstract JsonGenerator createJsonGenerator(OutputStream out, Charset enc)

Returns a new instance of a low-level JSON serializer for the given output stream and encoding.

Parameters
Name Description
out OutputStream

output stream

enc Charset

encoding

Returns
Type Description
JsonGenerator

new instance of a low-level JSON serializer

Exceptions
Type Description
IOException

createJsonGenerator(Writer writer)

public abstract JsonGenerator createJsonGenerator(Writer writer)

Returns a new instance of a low-level JSON serializer for the given writer.

Parameter
Name Description
writer Writer

writer

Returns
Type Description
JsonGenerator

new instance of a low-level JSON serializer

Exceptions
Type Description
IOException

createJsonObjectParser()

public final JsonObjectParser createJsonObjectParser()

Creates an object parser which uses this factory to parse JSON data.

Returns
Type Description
JsonObjectParser

createJsonParser(InputStream in)

public abstract JsonParser createJsonParser(InputStream in)

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.

Parameter
Name Description
in InputStream

input stream

Returns
Type Description
JsonParser

new instance of a low-level JSON parser

Exceptions
Type Description
IOException

createJsonParser(InputStream in, Charset charset)

public abstract JsonParser createJsonParser(InputStream in, Charset charset)

Returns a new instance of a low-level JSON parser for the given input stream.

Parameters
Name Description
in InputStream

input stream

charset Charset

charset in which the input stream is encoded or null to let the parser detect the charset

Returns
Type Description
JsonParser

new instance of a low-level JSON parser

Exceptions
Type Description
IOException

createJsonParser(Reader reader)

public abstract JsonParser createJsonParser(Reader reader)

Returns a new instance of a low-level JSON parser for the given reader.

Parameter
Name Description
reader Reader

reader

Returns
Type Description
JsonParser

new instance of a low-level JSON parser

Exceptions
Type Description
IOException

createJsonParser(String value)

public abstract JsonParser createJsonParser(String value)

Returns a new instance of a low-level JSON parser for the given string value.

Parameter
Name Description
value String

string value

Returns
Type Description
JsonParser

new instance of a low-level JSON parser

Exceptions
Type Description
IOException

toByteArray(Object item)

public final byte[] toByteArray(Object item)

Returns a UTF-8 encoded byte array of the serialized JSON representation for the given item using JsonGenerator#serialize(Object).

Parameter
Name Description
item Object

data key/value pairs

Returns
Type Description
byte[]

byte array of the serialized JSON representation

Exceptions
Type Description
IOException

toPrettyString(Object item)

public final String toPrettyString(Object item)

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.

Parameter
Name Description
item Object

data key/value pairs

Returns
Type Description
String

serialized JSON string representation

Exceptions
Type Description
IOException

toString(Object item)

public final String toString(Object item)

Returns a serialized JSON string representation for the given item using JsonGenerator#serialize(Object).

Parameter
Name Description
item Object

data key/value pairs

Returns
Type Description
String

serialized JSON string representation

Exceptions
Type Description
IOException