Class JsonWriter
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
DefaultJsonWriter
,GsonJsonWriter
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionabstract void
close()
Closes the JSON stream.abstract JsonWriter
flush()
Flushes any un-flushed content written to this writer.abstract JsonWriteContext
Gets the currentwriting context
for the JSON object.final <T> JsonWriter
writeArray
(Iterable<T> array, boolean writeNull, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array.final <T> JsonWriter
writeArray
(Iterable<T> array, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array.final <T> JsonWriter
writeArray
(T[] array, boolean writeNull, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array.final <T> JsonWriter
writeArray
(T[] array, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array.final <T> JsonWriter
writeArrayField
(String fieldName, Iterable<T> array, boolean writeNull, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array field.final <T> JsonWriter
writeArrayField
(String fieldName, Iterable<T> array, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array field.final <T> JsonWriter
writeArrayField
(String fieldName, T[] array, boolean writeNull, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array field.final <T> JsonWriter
writeArrayField
(String fieldName, T[] array, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array field.abstract JsonWriter
writeBinary
(byte[] value) Writes a JSON binary value.final JsonWriter
writeBinary
(byte[] value, boolean writeNull) Writes a JSON binary value.abstract JsonWriter
writeBinaryField
(String fieldName, byte[] value) Writes a JSON binary field.final JsonWriter
writeBinaryField
(String fieldName, byte[] value, boolean writeNull) Writes a JSON binary field.abstract JsonWriter
writeBoolean
(boolean value) Writes a JSON boolean value (true
orfalse
).final JsonWriter
writeBoolean
(Boolean value) Writes a nullable JSON boolean value (true
,false
, ornull
).final JsonWriter
writeBoolean
(Boolean value, boolean writeNull) Writes a nullable JSON boolean value (true
,false
, ornull
).abstract JsonWriter
writeBooleanField
(String fieldName, boolean value) Writes a JSON boolean field.final JsonWriter
writeBooleanField
(String fieldName, Boolean value) Writes a nullable JSON boolean field.final JsonWriter
writeBooleanField
(String fieldName, Boolean value, boolean writeNull) Writes a nullable JSON boolean field.abstract JsonWriter
writeDouble
(double value) Writes a JSON double value.final JsonWriter
writeDouble
(Double value) Writes a nullable JSON double value.final JsonWriter
writeDouble
(Double value, boolean writeNull) Writes a nullable JSON double value.abstract JsonWriter
writeDoubleField
(String fieldName, double value) Writes a JSON double field.final JsonWriter
writeDoubleField
(String fieldName, Double value) Writes a nullable JSON double field.final JsonWriter
writeDoubleField
(String fieldName, Double value, boolean writeNull) Writes a nullable JSON double field.abstract JsonWriter
Writes a JSON end array (]
).abstract JsonWriter
Writes a JSON end object (}
).abstract JsonWriter
writeFieldName
(String fieldName) Writes a JSON field name ("fieldName":
).abstract JsonWriter
writeFloat
(float value) Writes a JSON float value.final JsonWriter
writeFloat
(Float value) Writes a nullable JSON float value.final JsonWriter
writeFloat
(Float value, boolean writeNull) Writes a nullable JSON float value.abstract JsonWriter
writeFloatField
(String fieldName, float value) Writes a JSON float field.final JsonWriter
writeFloatField
(String fieldName, Float value) Writes a nullable JSON float field.final JsonWriter
writeFloatField
(String fieldName, Float value, boolean writeNull) Writes a nullable JSON float field.abstract JsonWriter
writeInt
(int value) Writes a JSON int value.final JsonWriter
writeInteger
(Integer value) Writes a nullable JSON int value.final JsonWriter
writeInteger
(Integer value, boolean writeNull) Writes a nullable JSON integer value.final JsonWriter
writeIntegerField
(String fieldName, Integer value) Writes a nullable JSON int field.final JsonWriter
writeIntegerField
(String fieldName, Integer value, boolean writeNull) Writes a nullable JSON int field.abstract JsonWriter
writeIntField
(String fieldName, int value) Writes a JSON int field.final JsonWriter
writeJson
(JsonSerializable<?> value) Writes aJsonSerializable
object.final JsonWriter
writeJson
(JsonSerializable<?> value, boolean writeNull) Writes aJsonSerializable
object.final JsonWriter
writeJsonField
(String fieldName, JsonSerializable<?> value) Writes aJsonSerializable
field.final JsonWriter
writeJsonField
(String fieldName, JsonSerializable<?> value, boolean writeNull) Writes aJsonSerializable
field.abstract JsonWriter
writeLong
(long value) Writes a JSON long value.final JsonWriter
Writes a nullable JSON long value.final JsonWriter
Writes a nullable JSON long value.abstract JsonWriter
writeLongField
(String fieldName, long value) Writes a JSON long field.final JsonWriter
writeLongField
(String fieldName, Long value) Writes a nullable JSON long field.final JsonWriter
writeLongField
(String fieldName, Long value, boolean writeNull) Writes a nullable JSON long field.final <T> JsonWriter
writeMap
(Map<String, T> map, boolean writeNull, BiConsumer<JsonWriter, T> valueWriterFunc) Writes a JSON map.final <T> JsonWriter
writeMap
(Map<String, T> map, BiConsumer<JsonWriter, T> valueWriterFunc) Writes a JSON map.final <T> JsonWriter
writeMapField
(String fieldName, Map<String, T> map, boolean writeNull, BiConsumer<JsonWriter, T> valueWriterFunc) Writes a JSON map field.final <T> JsonWriter
writeMapField
(String fieldName, Map<String, T> map, BiConsumer<JsonWriter, T> valueWriterFunc) Writes a JSON map field.abstract JsonWriter
Writes a JSON null.abstract JsonWriter
writeNullField
(String fieldName) Writes a JSON null field ("fieldName":null
).abstract JsonWriter
writeRawField
(String fieldName, String value) Writes the passed field literally without any additional handling.abstract JsonWriter
writeRawValue
(String value) Writes the passed value literally without any additional handling.abstract JsonWriter
Writes a JSON start array ([
).final JsonWriter
writeStartArray
(String fieldName) Writes a JSON start array ([
) with a preceding field name.abstract JsonWriter
Writes a JSON start object ({
).final JsonWriter
writeStartObject
(String fieldName) Writes a JSON start object ({
) with a preceding field name.abstract JsonWriter
writeString
(String value) Writes a JSON String value.final JsonWriter
writeString
(String value, boolean writeNull) Writes a nullable JSON String value.abstract JsonWriter
writeStringField
(String fieldName, String value) Writes a JSON String field.final JsonWriter
writeStringField
(String fieldName, String value, boolean writeNull) Writes a JSON String field.writeUntyped
(Object value) Writes the unknown typevalue
.writeUntypedField
(String fieldName, Object value) Writes the unknown typevalue
field.
-
Constructor Details
-
JsonWriter
public JsonWriter()
-
-
Method Details
-
getWriteContext
Gets the currentwriting context
for the JSON object.The writing context can help determine whether a write operation would be illegal.
The initial write context is
JsonWriteContext.ROOT
.- Returns:
- The current writing context.
-
close
Closes the JSON stream.If the
writing context
isn'tJsonWriteContext.COMPLETED
when this is called anIllegalStateException
will be thrown.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IllegalStateException
- If theJsonWriter
is closed before thewriting context
isJsonWriteContext.COMPLETED
.IOException
-
flush
Flushes any un-flushed content written to this writer.It should be assumed that each write call won't flush any contents.
- Returns:
- The flushed JsonWriter object.
-
writeStartObject
Writes a JSON start object ({
).- Returns:
- The updated JsonWriter object.
-
writeStartObject
Writes a JSON start object ({
) with a preceding field name.This API is the equivalent of calling
writeFieldName(String)
andwriteStartObject()
, in that order.- Parameters:
fieldName
- The field name.- Returns:
- The updated JsonWriter object.
-
writeEndObject
Writes a JSON end object (}
).If the current writing context isn't an object an
IllegalStateException
will be thrown.- Returns:
- The updated JsonWriter object.
-
writeStartArray
Writes a JSON start array ([
).- Returns:
- The updated JsonWriter object.
-
writeStartArray
Writes a JSON start array ([
) with a preceding field name.This API is the equivalent of calling
writeFieldName(String)
andwriteStartArray()
, in that order.- Parameters:
fieldName
- The field name.- Returns:
- The updated JsonWriter object.
-
writeEndArray
Writes a JSON end array (]
).- Returns:
- The updated JsonWriter object.
-
writeFieldName
Writes a JSON field name ("fieldName":
).- Parameters:
fieldName
- The field name.- Returns:
- The updated JsonWriter object.
-
writeJson
Writes aJsonSerializable
object.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, usewriteJson(JsonSerializable, boolean)
to indicate whether null should be written.This API is used instead of
writeJsonField(String, JsonSerializable)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Parameters:
value
-JsonSerializable
object to write.- Returns:
- The updated JsonWriter object.
-
writeArray
Writes a JSON array.This API will begin by writing the start array (
[
) followed by all elements in the array using theelementWriterFunc
and finishing by writing the end array (]
).If the passed
array
is null JSON null will be written. If null shouldn't be written usewriteArray(Object[], boolean, BiConsumer)
and pass false forwriteNull
.This API is used instead of
writeArrayField(String, Object[], BiConsumer)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Type Parameters:
T
- The array element type.- Parameters:
array
- The array being written.elementWriterFunc
- The function that writes each element of the array.- Returns:
- The updated JsonWriter object.
-
writeArray
public final <T> JsonWriter writeArray(T[] array, boolean writeNull, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array.This API will begin by writing the start array (
[
) followed by all elements in the array using theelementWriterFunc
and finishing by writing the end array (]
).writeNull
determines whether JSON null should be written ifarray
is null.This API is used instead of
writeArrayField(String, Object[], boolean, BiConsumer)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Type Parameters:
T
- The array element type.- Parameters:
array
- The array being written.writeNull
- Whether JSON null should be written ifarray
is null.elementWriterFunc
- The function that writes each element of the array.- Returns:
- The updated JsonWriter object.
-
writeArray
public final <T> JsonWriter writeArray(Iterable<T> array, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array.This API will begin by writing the start array (
[
) followed by all elements in the array using theelementWriterFunc
and finishing by writing the end array (]
).If the passed
array
is null JSON null will be written. If null shouldn't be written usewriteArray(Iterable, boolean, BiConsumer)
and pass false forwriteNull
.This API is used instead of
writeArrayField(String, Iterable, BiConsumer)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Type Parameters:
T
- The array element type.- Parameters:
array
- The array being written.elementWriterFunc
- The function that writes each element of the array.- Returns:
- The updated JsonWriter object.
-
writeArray
public final <T> JsonWriter writeArray(Iterable<T> array, boolean writeNull, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array.This API will begin by writing the start array (
[
) followed by all elements in the array using theelementWriterFunc
and finishing by writing the end array (]
).writeNull
determines whether JSON null should be written ifmap
is null.This API is used instead of
writeArrayField(String, Iterable, boolean, BiConsumer)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Type Parameters:
T
- The array element type.- Parameters:
array
- The array being written.writeNull
- Whether JSON null should be written ifarray
is null.elementWriterFunc
- The function that writes each element of the array.- Returns:
- The updated JsonWriter object.
-
writeMap
Writes a JSON map.This API will begin by writing the start object (
{
) followed by key-value fields in the map using thevalueWriterFunc
and finishing by writing the end object (}
).If the passed
map
is null JSON null will be written. If null shouldn't be written usewriteMap(Map, boolean, BiConsumer)
and pass false forwriteNull
.This API is used instead of
writeMapField(String, Map, BiConsumer)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Type Parameters:
T
- The value element type.- Parameters:
map
- The map being written.valueWriterFunc
- The function that writes value of each key-value pair in the map.- Returns:
- The updated JsonWriter object.
-
writeMap
public final <T> JsonWriter writeMap(Map<String, T> map, boolean writeNull, BiConsumer<JsonWriter, T> valueWriterFunc) Writes a JSON map.This API will begin by writing the start object (
{
) followed by key-value fields in the map using thevalueWriterFunc
and finishing by writing the end object (}
).writeNull
determines whether JSON null should be written ifmap
is null.This API is used instead of
writeMapField(String, Map, boolean, BiConsumer)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Type Parameters:
T
- The value element type.- Parameters:
map
- The map being written.writeNull
- Whether JSON null should be written ifmap
is null.valueWriterFunc
- The function that writes value of each key-value pair in the map.- Returns:
- The updated JsonWriter object.
-
writeJson
Writes aJsonSerializable
object.If
writeNull
is false andvalue
is null a value won't be written, effectively treating the call as a no-op.This API is used instead of
writeJsonField(String, JsonSerializable, boolean)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Parameters:
value
-JsonSerializable
object to write.writeNull
- Whether a null value should be written.- Returns:
- The updated JsonWriter object.
-
writeBinary
Writes a JSON binary value.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, usewriteBinary(byte[], boolean)
to indicate whether null should be written.This API is used instead of
writeBinaryField(String, byte[])
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Parameters:
value
- Binary value to write.- Returns:
- The updated JsonWriter object.
-
writeBinary
Writes a JSON binary value.If
writeNull
is false andvalue
is null a value won't be written, effectively treating the call as a no-op.This API is used instead of
writeBinaryField(String, byte[], boolean)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Parameters:
value
- Binary value to write.writeNull
- Whether a null value should be written.- Returns:
- The updated JsonWriter object.
-
writeBoolean
Writes a JSON boolean value (true
orfalse
).This API is used instead of
writeBooleanField(String, boolean)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.For the nullable
Boolean
usewriteBoolean(Boolean)
.- Parameters:
value
- boolean value to write.- Returns:
- The updated JsonWriter object.
-
writeBoolean
Writes a nullable JSON boolean value (true
,false
, ornull
).A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, usewriteBoolean(Boolean, boolean)
to indicate whether null should be written.This API is used instead of
writeBooleanField(String, Boolean)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.For the primitive
boolean
usewriteBoolean(boolean)
.- Parameters:
value
- Boolean value to write.- Returns:
- The updated JsonWriter object.
-
writeBoolean
Writes a nullable JSON boolean value (true
,false
, ornull
).If
writeNull
is false andvalue
is null a value won't be written, effectively treating the call as a no-op.This API is used instead of
writeBooleanField(String, Boolean, boolean)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Parameters:
value
- Boolean value to write.writeNull
- Whether a null value should be written.- Returns:
- The updated JsonWriter object.
-
writeDouble
Writes a JSON double value.This API is used instead of
writeDoubleField(String, double)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.For the nullable
Double
usewriteDouble(Double)
.- Parameters:
value
- double value to write.- Returns:
- The updated JsonWriter object.
-
writeDouble
Writes a nullable JSON double value.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, usewriteDouble(Double, boolean)
to indicate whether null should be written.This API is used instead of
writeDoubleField(String, Double)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.For the primitive
double
usewriteDouble(double)
.- Parameters:
value
- Double value to write.- Returns:
- The updated JsonWriter object.
-
writeDouble
Writes a nullable JSON double value.If
writeNull
is false andvalue
is null a value won't be written, effectively treating the call as a no-op.This API is used instead of
writeDoubleField(String, Double, boolean)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Parameters:
value
- Double value to write.writeNull
- Whether a null value should be written.- Returns:
- The updated JsonWriter object.
-
writeFloat
Writes a JSON float value.This API is used instead of
writeFloatField(String, float)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.For the nullable
Float
usewriteFloat(Float)
.- Parameters:
value
- float value to write.- Returns:
- The updated JsonWriter object.
-
writeFloat
Writes a nullable JSON float value.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, usewriteFloat(Float, boolean)
to indicate whether null should be written.This API is used instead of
writeFloatField(String, Float)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.For the primitive
float
usewriteFloat(float)
.- Parameters:
value
- Float value to write.- Returns:
- The updated JsonWriter object.
-
writeFloat
Writes a nullable JSON float value.If
writeNull
is false andvalue
is null a value won't be written, effectively treating the call as a no-op.This API is used instead of
writeFloatField(String, Float, boolean)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Parameters:
value
- Float value to write.writeNull
- Whether a null value should be written.- Returns:
- The updated JsonWriter object.
-
writeInt
Writes a JSON int value.This API is used instead of
writeIntField(String, int)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.For the nullable
Integer
usewriteInteger(Integer)
.- Parameters:
value
- int value to write.- Returns:
- The updated JsonWriter object.
-
writeInteger
Writes a nullable JSON int value.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, usewriteInteger(Integer, boolean)
to indicate whether null should be written.This API is used instead of
writeIntegerField(String, Integer)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.For the primitive
int
usewriteInt(int)
.- Parameters:
value
- Integer value to write.- Returns:
- The updated JsonWriter object.
-
writeInteger
Writes a nullable JSON integer value.If
writeNull
is false andvalue
is null a value won't be written, effectively treating the call as a no-op.This API is used instead of
writeIntegerField(String, Integer, boolean)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Parameters:
value
- Integer value to write.writeNull
- Whether a null value should be written.- Returns:
- The updated JsonWriter object.
-
writeLong
Writes a JSON long value.This API is used instead of
writeLongField(String, long)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.For the nullable
Long
usewriteLong(Long)
.- Parameters:
value
- long value to write.- Returns:
- The updated JsonWriter object.
-
writeLong
Writes a nullable JSON long value.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, usewriteLong(Long, boolean)
to indicate whether null should be written.This API is used instead of
writeLongField(String, Long)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.For the primitive
long
usewriteLong(long)
.- Parameters:
value
- Long value to write.- Returns:
- The updated JsonWriter object.
-
writeLong
Writes a nullable JSON long value.If
writeNull
is false andvalue
is null a value won't be written, effectively treating the call as a no-op.This API is used instead of
writeLongField(String, Long, boolean)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Parameters:
value
- Long value to write.writeNull
- Whether a null value should be written.- Returns:
- The updated JsonWriter object.
-
writeNull
Writes a JSON null.This API is used instead of
writeNullField(String)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Returns:
- The updated JsonWriter object.
-
writeString
Writes a JSON String value.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, usewriteString(String, boolean)
to indicate whether null should be written.This API is used instead of
writeStringField(String, String)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Parameters:
value
- String value to write.- Returns:
- The updated JsonWriter object.
-
writeString
Writes a nullable JSON String value.If
writeNull
is false andvalue
is null a value won't be written, effectively treating the call as a no-op.This API is used instead of
writeStringField(String, String, boolean)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Parameters:
value
- String value to write.writeNull
- Whether a null value should be written.- Returns:
- The updated JsonWriter object.
-
writeRawValue
Writes the passed value literally without any additional handling.Use this API when writing a String value that is already properly formatted JSON, such as a JSON string (
"\"string\""
), number ("42"
,"42.0"
), boolean ("true"
,"false"
), null ("null"
), array ("[\"string\", \"array\"]"
), or object ({"\"field\":\"value\""}
).This API is used instead of
writeRawField(String, String)
when the value needs to be written to the root of the JSON value, as an element in an array, or after a call towriteFieldName(String)
.- Parameters:
value
- The raw JSON value to write.- Returns:
- The updated JsonWriter object.
-
writeJsonField
Writes aJsonSerializable
field.Combines
writeFieldName(String)
andwriteJson(JsonSerializable)
to simplify adding a key-value to a JSON object.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, usewriteJsonField(String, JsonSerializable, boolean)
to indicate whether null should be written.- Parameters:
fieldName
- The field name.value
-JsonSerializable
object to write.- Returns:
- The updated JsonWriter object.
-
writeArrayField
public final <T> JsonWriter writeArrayField(String fieldName, T[] array, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array field.This API will begin by writing the field name and start array (
[
) followed by all elements in the array using theelementWriterFunc
and finishing by writing the end array (]
).If the passed
array
is null a JSON null field will be written. If a null field shouldn't be written usewriteArrayField(String, Object[], boolean, BiConsumer)
and pass false forwriteNull
.Combines
writeFieldName(String)
andwriteArray(Object[], BiConsumer)
to simplify adding a key-value to a JSON object.- Type Parameters:
T
- The array element type.- Parameters:
fieldName
- The field name.array
- The array being written.elementWriterFunc
- The function that writes each element of the array.- Returns:
- The updated JsonWriter object.
-
writeArrayField
public final <T> JsonWriter writeArrayField(String fieldName, T[] array, boolean writeNull, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array field.This API will begin by writing the field name and start array (
[
) followed by all elements in the array using theelementWriterFunc
and finishing by writing the end array (]
).writeNull
determines whether a JSON null field should be written ifarray
is null.Combines
writeFieldName(String)
andwriteArray(Object[], boolean, BiConsumer)
to simplify adding a key-value to a JSON object.- Type Parameters:
T
- The array element type.- Parameters:
fieldName
- The field name.array
- The array being written.writeNull
- Whether JSON null should be written ifarray
is null.elementWriterFunc
- The function that writes each element of the array.- Returns:
- The updated JsonWriter object.
-
writeArrayField
public final <T> JsonWriter writeArrayField(String fieldName, Iterable<T> array, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array field.This API will begin by writing the field name and start array (
[
) followed by all elements in the array using theelementWriterFunc
and finishing by writing the end array (]
).If the passed
array
is null a JSON null field will be written. If a null field shouldn't be written usewriteArrayField(String, Iterable, boolean, BiConsumer)
and pass false forwriteNull
.Combines
writeFieldName(String)
andwriteArray(Iterable, BiConsumer)
to simplify adding a key-value to a JSON object.- Type Parameters:
T
- The array element type.- Parameters:
fieldName
- The field name.array
- The array being written.elementWriterFunc
- The function that writes each element of the array.- Returns:
- The updated JsonWriter object.
-
writeArrayField
public final <T> JsonWriter writeArrayField(String fieldName, Iterable<T> array, boolean writeNull, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array field.This API will begin by writing the field name and start array (
[
) followed by all elements in the array using theelementWriterFunc
and finishing by writing the end array (]
).writeNull
determines whether a JSON null field should be written ifarray
is null.Combines
writeFieldName(String)
andwriteArray(Object[], boolean, BiConsumer)
to simplify adding a key-value to a JSON object.- Type Parameters:
T
- The array element type.- Parameters:
fieldName
- The field name.array
- The array being written.writeNull
- Whether JSON null should be written ifarray
is null.elementWriterFunc
- The function that writes each element of the array.- Returns:
- The updated JsonWriter object.
-
writeMapField
public final <T> JsonWriter writeMapField(String fieldName, Map<String, T> map, BiConsumer<JsonWriter, T> valueWriterFunc) Writes a JSON map field.This API will begin by writing the field name and start object (
{
) followed by key-value fields in the map using thevalueWriterFunc
and finishing by writing the end object (}
).If the passed
map
is null a JSON null field will be written. If a null field shouldn't be written usewriteMapField(String, Map, boolean, BiConsumer)
and pass false forwriteNull
.Combines
writeFieldName(String)
andwriteMap(Map, BiConsumer)
to simplify adding a key-value to a JSON object.- Type Parameters:
T
- The value element type.- Parameters:
fieldName
- The field name.map
- The map being written.valueWriterFunc
- The function that writes each value of the map.- Returns:
- The updated JsonWriter object.
-
writeMapField
public final <T> JsonWriter writeMapField(String fieldName, Map<String, T> map, boolean writeNull, BiConsumer<JsonWriter, T> valueWriterFunc) Writes a JSON map field.This API will begin by writing the field name and start object (
{
) followed by key-value fields in the map using thevalueWriterFunc
and finishing by writing the end object (}
).writeNull
determines whether a JSON null field should be written ifmap
is null.Combines
writeFieldName(String)
andwriteMap(Map, boolean, BiConsumer)
to simplify adding a key-value to a JSON object.- Type Parameters:
T
- The value element type.- Parameters:
fieldName
- The field name.map
- The map being written.writeNull
- Whether JSON null should be written ifmap
is null.valueWriterFunc
- The function that writes each value of the map.- Returns:
- The updated JsonWriter object.
-
writeJsonField
public final JsonWriter writeJsonField(String fieldName, JsonSerializable<?> value, boolean writeNull) Writes aJsonSerializable
field.Combines
writeFieldName(String)
andwriteJson(JsonSerializable)
to simplify adding a key-value to a JSON object.If
writeNull
is false andvalue
is null neither a field name or a field value will be written, effectively treating the call as a no-op.- Parameters:
fieldName
- The field name.value
-JsonSerializable
object to write.writeNull
- Whether a null field should be written.- Returns:
- The updated JsonWriter object, or if
writeNull
is false andvalue
is null the unmodified JsonWriter.
-
writeBinaryField
Writes a JSON binary field.Combines
writeFieldName(String)
andwriteBinary(byte[])
to simplify adding a key-value to a JSON object.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, usewriteBinaryField(String, byte[], boolean)
to indicate whether null should be written.- Parameters:
fieldName
- The field name.value
- Binary value to write.- Returns:
- The updated JsonWriter object.
-
writeBinaryField
Writes a JSON binary field.Combines
writeFieldName(String)
andwriteBinary(byte[])
to simplify adding a key-value to a JSON object.If
writeNull
is false andvalue
is null neither a field name or a field value will be written, effectively treating the call as a no-op.- Parameters:
fieldName
- The field name.value
- Binary value to write.writeNull
- Whether a null field should be written.- Returns:
- The updated JsonWriter object, or if
writeNull
is false andvalue
is null the unmodified JsonWriter.
-
writeBooleanField
Writes a JSON boolean field.Combines
writeFieldName(String)
andwriteBoolean(boolean)
to simplify adding a key-value to a JSON object.- Parameters:
fieldName
- The field name.value
- boolean value to write.- Returns:
- The updated JsonWriter object.
-
writeBooleanField
Writes a nullable JSON boolean field.Combines
writeFieldName(String)
andwriteBoolean(Boolean)
to simplify adding a key-value to a JSON object.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, usewriteBooleanField(String, Boolean, boolean)
to indicate whether null should be written.- Parameters:
fieldName
- The field name.value
- Boolean value to write.- Returns:
- The updated JsonWriter object.
-
writeBooleanField
Writes a nullable JSON boolean field.Combines
writeFieldName(String)
andwriteBoolean(Boolean)
to simplify adding a key-value to a JSON object.If
writeNull
is false andvalue
is null neither a field name or a field value will be written, effectively treating the call as a no-op.- Parameters:
fieldName
- The field name.value
- Boolean value to write.writeNull
- Whether a null field should be written.- Returns:
- The updated JsonWriter object, or if
writeNull
is false andvalue
is null the unmodified JsonWriter.
-
writeDoubleField
Writes a JSON double field.Combines
writeFieldName(String)
andwriteDouble(double)
to simplify adding a key-value to a JSON object.- Parameters:
fieldName
- The field name.value
- double value to write.- Returns:
- The updated JsonWriter object.
-
writeDoubleField
Writes a nullable JSON double field.Combines
writeFieldName(String)
andwriteDouble(Double)
to simplify adding a key-value to a JSON object.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, usewriteDoubleField(String, Double, boolean)
to indicate whether null should be written.- Parameters:
fieldName
- The field name.value
- Double value to write.- Returns:
- The updated JsonWriter object.
-
writeDoubleField
Writes a nullable JSON double field.Combines
writeFieldName(String)
andwriteDouble(Double)
to simplify adding a key-value to a JSON object.If
writeNull
is false andvalue
is null neither a field name or a field value will be written, effectively treating the call as a no-op.- Parameters:
fieldName
- The field name.value
- Double value to write.writeNull
- Whether a null field should be written.- Returns:
- The updated JsonWriter object, or if
writeNull
is false andvalue
is null the unmodified JsonWriter.
-
writeFloatField
Writes a JSON float field.Combines
writeFieldName(String)
andwriteFloat(float)
to simplify adding a key-value to a JSON object.- Parameters:
fieldName
- The field name.value
- float value to write.- Returns:
- The updated JsonWriter object.
-
writeFloatField
Writes a nullable JSON float field.Combines
writeFieldName(String)
andwriteFloat(Float)
to simplify adding a key-value to a JSON object.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, usewriteFloatField(String, Float, boolean)
to indicate whether null should be written.- Parameters:
fieldName
- The field name.value
- Float value to write.- Returns:
- The updated JsonWriter object.
-
writeFloatField
Writes a nullable JSON float field.Combines
writeFieldName(String)
andwriteFloat(Float)
to simplify adding a key-value to a JSON object.If
writeNull
is false andvalue
is null neither a field name or a field value will be written, effectively treating the call as a no-op.- Parameters:
fieldName
- The field name.value
- Float value to write.writeNull
- Whether a null field should be written.- Returns:
- The updated JsonWriter object, or if
writeNull
is false andvalue
is null the unmodified JsonWriter.
-
writeIntField
Writes a JSON int field.Combines
writeFieldName(String)
andwriteInt(int)
to simplify adding a key-value to a JSON object.- Parameters:
fieldName
- The field name.value
- int value to write.- Returns:
- The updated JsonWriter object.
-
writeIntegerField
Writes a nullable JSON int field.Combines
writeFieldName(String)
andwriteInteger(Integer)
to simplify adding a key-value to a JSON object.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, usewriteIntegerField(String, Integer, boolean)
to indicate whether null should be written.- Parameters:
fieldName
- The field name.value
- Integer value to write.- Returns:
- The updated JsonWriter object.
-
writeIntegerField
Writes a nullable JSON int field.Combines
writeFieldName(String)
andwriteInteger(Integer)
to simplify adding a key-value to a JSON object.If
writeNull
is false andvalue
is null neither a field name or a field value will be written, effectively treating the call as a no-op.- Parameters:
fieldName
- The field name.value
- Integer value to write.writeNull
- Whether a null field should be written.- Returns:
- The updated JsonWriter object, or if
writeNull
is false andvalue
is null the unmodified JsonWriter.
-
writeLongField
Writes a JSON long field.Combines
writeFieldName(String)
andwriteLong(long)
to simplify adding a key-value to a JSON object.- Parameters:
fieldName
- The field name.value
- long value to write.- Returns:
- The updated JsonWriter object.
-
writeLongField
Writes a nullable JSON long field.Combines
writeFieldName(String)
andwriteLong(Long)
to simplify adding a key-value to a JSON object.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, usewriteLongField(String, Long, boolean)
to indicate whether null should be written.- Parameters:
fieldName
- The field name.value
- Long value to write.- Returns:
- The updated JsonWriter object.
-
writeLongField
Writes a nullable JSON long field.Combines
writeFieldName(String)
andwriteLong(Long)
to simplify adding a key-value to a JSON object.If
writeNull
is false andvalue
is null neither a field name or a field value will be written, effectively treating the call as a no-op.- Parameters:
fieldName
- The field name.value
- Long value to write.writeNull
- Whether a null field should be written.- Returns:
- The updated JsonWriter object, or if
writeNull
is false andvalue
is null the unmodified JsonWriter.
-
writeNullField
Writes a JSON null field ("fieldName":null
).Combines
writeFieldName(String)
andwriteNull()
to simplify adding a key-value to a JSON object.- Parameters:
fieldName
- The field name.- Returns:
- The updated JsonWriter object.
-
writeStringField
Writes a JSON String field.Combines
writeFieldName(String)
andwriteString(String)
to simplify adding a key-value to a JSON object.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, usewriteStringField(String, String, boolean)
to indicate whether null should be written.- Parameters:
fieldName
- The field name.value
- String value to write.- Returns:
- The updated JsonWriter object.
-
writeStringField
Writes a JSON String field.Combines
writeFieldName(String)
andwriteString(String)
to simplify adding a key-value to a JSON object.If
writeNull
is false andvalue
is null neither a field name or a field value will be written, effectively treating the call as a no-op.- Parameters:
fieldName
- The field name.value
- String value to write.writeNull
- Whether a null field should be written.- Returns:
- The updated JsonWriter object, or if
writeNull
is false andvalue
is null the unmodified JsonWriter.
-
writeRawField
Writes the passed field literally without any additional handling.Use this API when writing a String value that is already properly formatted JSON, such as a JSON string (
"\"string\""
), number ("42"
,"42.0"
), boolean ("true"
,"false"
), null ("null"
), array ("[\"string\", \"array\"]"
), or object ({"\"field\":\"value\""}
).Combines
writeFieldName(String)
andwriteRawValue(String)
to simplify adding a key-value to a JSON object.- Parameters:
fieldName
- The field name.value
- The raw JSON value to write.- Returns:
- The updated JsonWriter object.
-
writeUntypedField
Writes the unknown typevalue
field.The following is how each
value
is handled (in this order):- null ->
writeNull()
short
->writeInt(int)
int
->writeInt(int)
long
->writeLong(long)
float
->writeFloat(float)
double
->writeDouble(double)
boolean
->writeBoolean(boolean)
CharSequence
->writeString(String)
char
->writeString(String)
JsonSerializable
->writeJson(JsonSerializable)
Object[]
->writeUntyped(Object)
for each elementIterable
->writeUntyped(Object)
for each elementMap
->writeUntyped(Object)
for each element where the key totoString
'dObject
-> empty JSON object ({}
)- All other values use the
toString
value withwriteString(String)
- Parameters:
fieldName
- The field name.value
- The value to write.- Returns:
- The updated JsonWriter object.
- null ->
-
writeUntyped
Writes the unknown typevalue
.The following is how each
value
is handled (in this order):- null ->
writeNull()
short
->writeInt(int)
int
->writeInt(int)
long
->writeLong(long)
float
->writeFloat(float)
double
->writeDouble(double)
boolean
->writeBoolean(boolean)
CharSequence
->writeString(String)
char
->writeString(String)
JsonSerializable
->writeJson(JsonSerializable)
Object[]
->writeUntyped(Object)
for each elementIterable
->writeUntyped(Object)
for each elementMap
->writeUntyped(Object)
for each element where the key totoString
'dObject
-> empty JSON object ({}
)- All other values use the
toString
value withwriteString(String)
- Parameters:
value
- The value to write.- Returns:
- The updated JsonWriter object.
- null ->
-