Class JsonWriter
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
DefaultJsonWriter,GsonJsonWriter
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract voidclose()Closes the JSON stream.abstract JsonWriterflush()Flushes any un-flushed content written to this writer.abstract JsonWriteContextGets the currentwriting contextfor the JSON object.final <T> JsonWriterwriteArray(Iterable<T> array, boolean writeNull, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array.final <T> JsonWriterwriteArray(Iterable<T> array, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array.final <T> JsonWriterwriteArray(T[] array, boolean writeNull, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array.final <T> JsonWriterwriteArray(T[] array, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array.final <T> JsonWriterwriteArrayField(String fieldName, Iterable<T> array, boolean writeNull, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array field.final <T> JsonWriterwriteArrayField(String fieldName, Iterable<T> array, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array field.final <T> JsonWriterwriteArrayField(String fieldName, T[] array, boolean writeNull, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array field.final <T> JsonWriterwriteArrayField(String fieldName, T[] array, BiConsumer<JsonWriter, T> elementWriterFunc) Writes a JSON array field.abstract JsonWriterwriteBinary(byte[] value) Writes a JSON binary value.final JsonWriterwriteBinary(byte[] value, boolean writeNull) Writes a JSON binary value.abstract JsonWriterwriteBinaryField(String fieldName, byte[] value) Writes a JSON binary field.final JsonWriterwriteBinaryField(String fieldName, byte[] value, boolean writeNull) Writes a JSON binary field.abstract JsonWriterwriteBoolean(boolean value) Writes a JSON boolean value (trueorfalse).final JsonWriterwriteBoolean(Boolean value) Writes a nullable JSON boolean value (true,false, ornull).final JsonWriterwriteBoolean(Boolean value, boolean writeNull) Writes a nullable JSON boolean value (true,false, ornull).abstract JsonWriterwriteBooleanField(String fieldName, boolean value) Writes a JSON boolean field.final JsonWriterwriteBooleanField(String fieldName, Boolean value) Writes a nullable JSON boolean field.final JsonWriterwriteBooleanField(String fieldName, Boolean value, boolean writeNull) Writes a nullable JSON boolean field.abstract JsonWriterwriteDouble(double value) Writes a JSON double value.final JsonWriterwriteDouble(Double value) Writes a nullable JSON double value.final JsonWriterwriteDouble(Double value, boolean writeNull) Writes a nullable JSON double value.abstract JsonWriterwriteDoubleField(String fieldName, double value) Writes a JSON double field.final JsonWriterwriteDoubleField(String fieldName, Double value) Writes a nullable JSON double field.final JsonWriterwriteDoubleField(String fieldName, Double value, boolean writeNull) Writes a nullable JSON double field.abstract JsonWriterWrites a JSON end array (]).abstract JsonWriterWrites a JSON end object (}).abstract JsonWriterwriteFieldName(String fieldName) Writes a JSON field name ("fieldName":).abstract JsonWriterwriteFloat(float value) Writes a JSON float value.final JsonWriterwriteFloat(Float value) Writes a nullable JSON float value.final JsonWriterwriteFloat(Float value, boolean writeNull) Writes a nullable JSON float value.abstract JsonWriterwriteFloatField(String fieldName, float value) Writes a JSON float field.final JsonWriterwriteFloatField(String fieldName, Float value) Writes a nullable JSON float field.final JsonWriterwriteFloatField(String fieldName, Float value, boolean writeNull) Writes a nullable JSON float field.abstract JsonWriterwriteInt(int value) Writes a JSON int value.final JsonWriterwriteInteger(Integer value) Writes a nullable JSON int value.final JsonWriterwriteInteger(Integer value, boolean writeNull) Writes a nullable JSON integer value.final JsonWriterwriteIntegerField(String fieldName, Integer value) Writes a nullable JSON int field.final JsonWriterwriteIntegerField(String fieldName, Integer value, boolean writeNull) Writes a nullable JSON int field.abstract JsonWriterwriteIntField(String fieldName, int value) Writes a JSON int field.final JsonWriterwriteJson(JsonSerializable<?> value) Writes aJsonSerializableobject.final JsonWriterwriteJson(JsonSerializable<?> value, boolean writeNull) Writes aJsonSerializableobject.final JsonWriterwriteJsonField(String fieldName, JsonSerializable<?> value) Writes aJsonSerializablefield.final JsonWriterwriteJsonField(String fieldName, JsonSerializable<?> value, boolean writeNull) Writes aJsonSerializablefield.abstract JsonWriterwriteLong(long value) Writes a JSON long value.final JsonWriterWrites a nullable JSON long value.final JsonWriterWrites a nullable JSON long value.abstract JsonWriterwriteLongField(String fieldName, long value) Writes a JSON long field.final JsonWriterwriteLongField(String fieldName, Long value) Writes a nullable JSON long field.final JsonWriterwriteLongField(String fieldName, Long value, boolean writeNull) Writes a nullable JSON long field.final <T> JsonWriterwriteMap(Map<String, T> map, boolean writeNull, BiConsumer<JsonWriter, T> valueWriterFunc) Writes a JSON map.final <T> JsonWriterwriteMap(Map<String, T> map, BiConsumer<JsonWriter, T> valueWriterFunc) Writes a JSON map.final <T> JsonWriterwriteMapField(String fieldName, Map<String, T> map, boolean writeNull, BiConsumer<JsonWriter, T> valueWriterFunc) Writes a JSON map field.final <T> JsonWriterwriteMapField(String fieldName, Map<String, T> map, BiConsumer<JsonWriter, T> valueWriterFunc) Writes a JSON map field.abstract JsonWriterWrites a JSON null.abstract JsonWriterwriteNullField(String fieldName) Writes a JSON null field ("fieldName":null).abstract JsonWriterwriteRawField(String fieldName, String value) Writes the passed field literally without any additional handling.abstract JsonWriterwriteRawValue(String value) Writes the passed value literally without any additional handling.abstract JsonWriterWrites a JSON start array ([).final JsonWriterwriteStartArray(String fieldName) Writes a JSON start array ([) with a preceding field name.abstract JsonWriterWrites a JSON start object ({).final JsonWriterwriteStartObject(String fieldName) Writes a JSON start object ({) with a preceding field name.abstract JsonWriterwriteString(String value) Writes a JSON String value.final JsonWriterwriteString(String value, boolean writeNull) Writes a nullable JSON String value.abstract JsonWriterwriteStringField(String fieldName, String value) Writes a JSON String field.final JsonWriterwriteStringField(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 typevaluefield.
-
Constructor Details
-
JsonWriter
public JsonWriter()
-
-
Method Details
-
getWriteContext
Gets the currentwriting contextfor 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 contextisn'tJsonWriteContext.COMPLETEDwhen this is called anIllegalStateExceptionwill be thrown.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IllegalStateException- If theJsonWriteris closed before thewriting contextisJsonWriteContext.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
IllegalStateExceptionwill 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 aJsonSerializableobject.A value is always written no matter whether
valueis 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-JsonSerializableobject 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 theelementWriterFuncand finishing by writing the end array (]).If the passed
arrayis 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 theelementWriterFuncand finishing by writing the end array (]).writeNulldetermines whether JSON null should be written ifarrayis 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 ifarrayis 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 theelementWriterFuncand finishing by writing the end array (]).If the passed
arrayis 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 theelementWriterFuncand finishing by writing the end array (]).writeNulldetermines whether JSON null should be written ifmapis 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 ifarrayis 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 thevalueWriterFuncand finishing by writing the end object (}).If the passed
mapis 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 thevalueWriterFuncand finishing by writing the end object (}).writeNulldetermines whether JSON null should be written ifmapis 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 ifmapis null.valueWriterFunc- The function that writes value of each key-value pair in the map.- Returns:
- The updated JsonWriter object.
-
writeJson
Writes aJsonSerializableobject.If
writeNullis false andvalueis 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-JsonSerializableobject 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
valueis 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
writeNullis false andvalueis 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 (trueorfalse).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
BooleanusewriteBoolean(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
valueis 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
booleanusewriteBoolean(boolean).- Parameters:
value- Boolean value to write.- Returns:
- The updated JsonWriter object.
-
writeBoolean
Writes a nullable JSON boolean value (true,false, ornull).If
writeNullis false andvalueis 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
DoubleusewriteDouble(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
valueis 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
doubleusewriteDouble(double).- Parameters:
value- Double value to write.- Returns:
- The updated JsonWriter object.
-
writeDouble
Writes a nullable JSON double value.If
writeNullis false andvalueis 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
FloatusewriteFloat(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
valueis 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
floatusewriteFloat(float).- Parameters:
value- Float value to write.- Returns:
- The updated JsonWriter object.
-
writeFloat
Writes a nullable JSON float value.If
writeNullis false andvalueis 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
IntegerusewriteInteger(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
valueis 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
intusewriteInt(int).- Parameters:
value- Integer value to write.- Returns:
- The updated JsonWriter object.
-
writeInteger
Writes a nullable JSON integer value.If
writeNullis false andvalueis 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
LongusewriteLong(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
valueis 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
longusewriteLong(long).- Parameters:
value- Long value to write.- Returns:
- The updated JsonWriter object.
-
writeLong
Writes a nullable JSON long value.If
writeNullis false andvalueis 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
valueis 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
writeNullis false andvalueis 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 aJsonSerializablefield.Combines
writeFieldName(String)andwriteJson(JsonSerializable)to simplify adding a key-value to a JSON object.A value is always written no matter whether
valueis 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-JsonSerializableobject 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 theelementWriterFuncand finishing by writing the end array (]).If the passed
arrayis 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 theelementWriterFuncand finishing by writing the end array (]).writeNulldetermines whether a JSON null field should be written ifarrayis 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 ifarrayis 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 theelementWriterFuncand finishing by writing the end array (]).If the passed
arrayis 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 theelementWriterFuncand finishing by writing the end array (]).writeNulldetermines whether a JSON null field should be written ifarrayis 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 ifarrayis 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 thevalueWriterFuncand finishing by writing the end object (}).If the passed
mapis 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 thevalueWriterFuncand finishing by writing the end object (}).writeNulldetermines whether a JSON null field should be written ifmapis 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 ifmapis 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 aJsonSerializablefield.Combines
writeFieldName(String)andwriteJson(JsonSerializable)to simplify adding a key-value to a JSON object.If
writeNullis false andvalueis 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-JsonSerializableobject to write.writeNull- Whether a null field should be written.- Returns:
- The updated JsonWriter object, or if
writeNullis false andvalueis 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
valueis 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
writeNullis false andvalueis 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
writeNullis false andvalueis 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
valueis 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
writeNullis false andvalueis 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
writeNullis false andvalueis 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
valueis 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
writeNullis false andvalueis 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
writeNullis false andvalueis 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
valueis 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
writeNullis false andvalueis 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
writeNullis false andvalueis 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
valueis 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
writeNullis false andvalueis 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
writeNullis false andvalueis 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
valueis 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
writeNullis false andvalueis 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
writeNullis false andvalueis 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
valueis 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
writeNullis false andvalueis 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
writeNullis false andvalueis 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 typevaluefield.The following is how each
valueis 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
toStringvalue 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
valueis 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
toStringvalue withwriteString(String)
- Parameters:
value- The value to write.- Returns:
- The updated JsonWriter object.
- null ->
-