Class JsonFactory (1.41.8)

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
NameDescription
inputStreamInputStream

JSON value in an input stream

destinationClassClass<T>

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

Returns
TypeDescription
T

new instance of the parsed destination class

Exceptions
TypeDescription
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
NameDescription
inputStreamInputStream

JSON value in an input stream

charsetCharset

Charset in which the stream is encoded

destinationClassClass<T>

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

Returns
TypeDescription
T

new instance of the parsed destination class

Exceptions
TypeDescription
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
NameDescription
readerReader

JSON value in a reader

destinationClassClass<T>

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

Returns
TypeDescription
T

new instance of the parsed destination class

Exceptions
TypeDescription
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
NameDescription
valueString

JSON string value

destinationClassClass<T>

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

Returns
TypeDescription
T

new instance of the parsed destination class

Exceptions
TypeDescription
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
NameDescription
outOutputStream

output stream

encCharset

encoding

Returns
TypeDescription
JsonGenerator

new instance of a low-level JSON serializer

Exceptions
TypeDescription
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
NameDescription
writerWriter

writer

Returns
TypeDescription
JsonGenerator

new instance of a low-level JSON serializer

Exceptions
TypeDescription
IOException

createJsonObjectParser()

public final JsonObjectParser createJsonObjectParser()

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

Returns
TypeDescription
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
NameDescription
inInputStream

input stream

Returns
TypeDescription
JsonParser

new instance of a low-level JSON parser

Exceptions
TypeDescription
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
NameDescription
inInputStream

input stream

charsetCharset

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

Returns
TypeDescription
JsonParser

new instance of a low-level JSON parser

Exceptions
TypeDescription
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
NameDescription
readerReader

reader

Returns
TypeDescription
JsonParser

new instance of a low-level JSON parser

Exceptions
TypeDescription
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
NameDescription
valueString

string value

Returns
TypeDescription
JsonParser

new instance of a low-level JSON parser

Exceptions
TypeDescription
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
NameDescription
itemObject

data key/value pairs

Returns
TypeDescription
byte[]

byte array of the serialized JSON representation

Exceptions
TypeDescription
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
NameDescription
itemObject

data key/value pairs

Returns
TypeDescription
String

serialized JSON string representation

Exceptions
TypeDescription
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
NameDescription
itemObject

data key/value pairs

Returns
TypeDescription
String

serialized JSON string representation

Exceptions
TypeDescription
IOException