Class JsonWriter
- All Implemented Interfaces:
Closeable,AutoCloseable
Instances of JsonWriter are created using an instance of JsonProvider or using the utility methods
in JsonProviders.
- See Also:
-
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, WriteValueCallback<JsonWriter, T> elementWriterFunc) Writes a JSON array.final <T> JsonWriterwriteArray(T[] array, WriteValueCallback<JsonWriter, T> elementWriterFunc) Writes a JSON array.final <T> JsonWriterwriteArrayField(String fieldName, Iterable<T> array, WriteValueCallback<JsonWriter, T> elementWriterFunc) Writes a JSON array field.final <T> JsonWriterwriteArrayField(String fieldName, T[] array, WriteValueCallback<JsonWriter, T> elementWriterFunc) Writes a JSON array field.abstract JsonWriterwriteBinary(byte[] value) Writes a JSON binary value.final JsonWriterwriteBinaryField(String fieldName, byte[] value) 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 JsonWriterwriteBooleanField(String fieldName, boolean value) Writes a JSON boolean field.final JsonWriterwriteBooleanField(String fieldName, Boolean value) Writes a nullable JSON boolean field.abstract JsonWriterwriteDouble(double value) Writes a JSON double value.final JsonWriterwriteDoubleField(String fieldName, double value) Writes a 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 JsonWriterwriteFloatField(String fieldName, float value) Writes a JSON float field.abstract JsonWriterwriteInt(int value) Writes a JSON int value.final JsonWriterwriteIntField(String fieldName, int value) Writes a JSON int field.final JsonWriterwriteJson(JsonSerializable<?> value) Writes aJsonSerializableobject.final JsonWriterwriteJsonField(String fieldName, JsonSerializable<?> value) Writes aJsonSerializablefield.abstract JsonWriterwriteLong(long value) Writes a JSON long value.final JsonWriterwriteLongField(String fieldName, long value) Writes a JSON long field.final <T> JsonWriterwriteMap(Map<String, T> map, WriteValueCallback<JsonWriter, T> valueWriterFunc) Writes a JSON map.final <T> JsonWriterwriteMapField(String fieldName, Map<String, T> map, WriteValueCallback<JsonWriter, T> valueWriterFunc) Writes a JSON map field.abstract JsonWriterWrites a JSON null.final <T> JsonWriterwriteNullableField(String fieldName, T nullable, WriteValueCallback<JsonWriter, T> writerFunc) Writes a nullable JSON field.final JsonWriterwriteNullField(String fieldName) Writes a JSON null field ("fieldName":null).final JsonWriterwriteNumber(Number value) Writes a nullable JSON number value.final JsonWriterwriteNumberField(String fieldName, Number value) Writes a nullable JSON number field.final 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 JsonWriterwriteStringField(String fieldName, String value) 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()Creates an instance ofJsonWriter.
-
-
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.During closing the implementation of
JsonWritermust flush any un-flushed content.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IllegalStateException- If theJsonWriteris closed before thewriting contextisJsonWriteContext.COMPLETED.IOException- If the underlying content store fails to close.
-
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.
- Throws:
IOException- If the underlying content fails to flush.
-
writeStartObject
Writes a JSON start object ({).- Returns:
- The updated JsonWriter object.
- Throws:
IOException- If JSON start object fails to be written.
-
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.
- Throws:
NullPointerException- IffieldNameis null.IOException- If eitherfieldNameor JSON start object fails to be written.
-
writeEndObject
Writes a JSON end object (}).If the current writing context isn't an object an
IllegalStateExceptionwill be thrown.- Returns:
- The updated JsonWriter object.
- Throws:
IOException- If JSON end object fails to be written.
-
writeStartArray
Writes a JSON start array ([).- Returns:
- The updated JsonWriter object.
- Throws:
IOException- If JSON start array fails to be written.
-
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.
- Throws:
NullPointerException- IffieldNameis null.IOException- If eitherfieldNameor JSON start array fails to be written.
-
writeEndArray
Writes a JSON end array (]).- Returns:
- The updated JsonWriter object.
- Throws:
IOException- If JSON end array fails to be written.
-
writeFieldName
Writes a JSON field name ("fieldName":).- Parameters:
fieldName- The field name.- Returns:
- The updated JsonWriter object.
- Throws:
NullPointerException- IffieldNameis null.IOException- IffieldNamefails to be written.
-
writeJson
Writes aJsonSerializableobject.If
valueis nullJsonToken.NULLwill 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.
- Throws:
IOException- If theJsonSerializablefails to be written.
-
writeArray
public final <T> JsonWriter writeArray(T[] array, WriteValueCallback<JsonWriter, T> elementWriterFunc) throws IOExceptionWrites 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
arrayis nullJsonToken.NULLwill be written.This API is used instead of
writeArrayField(String, Object[], WriteValueCallback)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.
- Throws:
NullPointerException- IfelementWriterFuncis null.IOException- If the JSON array fails to be written, either the start or end array or an element write.
-
writeArray
public final <T> JsonWriter writeArray(Iterable<T> array, WriteValueCallback<JsonWriter, T> elementWriterFunc) throws IOExceptionWrites 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
arrayis nullJsonToken.NULLwill be written.This API is used instead of
writeArrayField(String, Iterable, WriteValueCallback)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.
- Throws:
NullPointerException- IfelementWriterFuncis null.IOException- If the JSON array fails to be written, either the start or end array or an element write.
-
writeMap
public final <T> JsonWriter writeMap(Map<String, T> map, WriteValueCallback<JsonWriter, throws IOExceptionT> 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 (}).If
mapis nullJsonToken.NULLwill be written.This API is used instead of
writeMapField(String, Map, WriteValueCallback)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.
- Throws:
NullPointerException- IfvalueWriterFuncis null.IOException- If the JSON map fails to be written, either the start or end object or a key or value write.
-
writeBinary
Writes a JSON binary value.If
valueis nullJsonToken.NULLwill 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.
- Throws:
IOException- If the binaryvaluefails to be written.
-
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.
- Throws:
IOException- If the booleanvaluefails to be written.
-
writeBoolean
Writes a nullable JSON boolean value (true,false, ornull).If
valueis nullJsonToken.NULLwill 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.
- Throws:
IOException- If the Booleanvaluefails to be written.
-
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
DoubleusewriteNumber(Number).- Parameters:
value- double value to write.- Returns:
- The updated JsonWriter object.
- Throws:
IOException- If the doublevaluefails to be written.
-
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
FloatusewriteNumber(Number).- Parameters:
value- float value to write.- Returns:
- The updated JsonWriter object.
- Throws:
IOException- If the floatvaluefails to be written.
-
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
IntegerusewriteNumber(Number).- Parameters:
value- int value to write.- Returns:
- The updated JsonWriter object.
- Throws:
IOException- If the intvaluefails to be written.
-
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
LongusewriteNumber(Number).- Parameters:
value- long value to write.- Returns:
- The updated JsonWriter object.
- Throws:
IOException- If the longvaluefails to be written.
-
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.
- Throws:
IOException- If a JSON null fails to be written.
-
writeNumber
Writes a nullable JSON number value.If
valueis nullJsonToken.NULLwill be written.This API is used instead of
writeNumberField(String, Number)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- Number value to write.- Returns:
- The updated JsonWriter object.
- Throws:
IOException- If the Numbervaluefails to be written.
-
writeString
Writes a JSON String value.If
valueis nullJsonToken.NULLwill 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.
- Throws:
IOException- If the Stringvaluefails to be written.
-
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.
- Throws:
NullPointerException- Ifvalueis null.IOException- If the rawvaluefails to be written.
-
writeNullableField
public final <T> JsonWriter writeNullableField(String fieldName, T nullable, WriteValueCallback<JsonWriter, T> writerFunc) throws IOExceptionWrites a nullable JSON field.When the
valueis null this effectively is the same aswriteNullField(String). When thevalueisn't null this will write the JSON field name and call thewriterFuncthat is supplied with the non-null nullable value and thisJsonWriterto perform the write operation.- Type Parameters:
T- Type of the nullable value.- Parameters:
fieldName- The field name.nullable- The nullable JSON value.writerFunc- The non-null JSON value writer function.- Returns:
- The updated JsonWriter object.
- Throws:
NullPointerException- IffieldNameorwriterFuncis null.IOException- If thefieldNameor thenullablefails to be written.
-
writeJsonField
public final JsonWriter writeJsonField(String fieldName, JsonSerializable<?> value) throws IOException Writes aJsonSerializablefield.Combines
writeFieldName(String)andwriteJson(JsonSerializable)to simplify adding a key-value to a JSON object.The field is only written when
valueisn't null, if a null field needs to be written usewriteNullableField(String, Object, WriteValueCallback).- Parameters:
fieldName- The field name.value-JsonSerializableobject to write.- Returns:
- The updated JsonWriter object.
- Throws:
NullPointerException- IffieldNameis null.IOException- If thefieldNameor theJsonSerializablefails to be written.
-
writeArrayField
public final <T> JsonWriter writeArrayField(String fieldName, T[] array, WriteValueCallback<JsonWriter, T> elementWriterFunc) throws IOExceptionWrites 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 (]).The field is only written when
valueisn't null, if a null field needs to be written usewriteNullableField(String, Object, WriteValueCallback).Combines
writeFieldName(String)andwriteArray(Object[], WriteValueCallback)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.
- Throws:
NullPointerException- IffieldNameorelementWriterFuncis null.IOException- If thefieldNameor the JSON array fails to be written, either JSON start or end array or the element write.
-
writeArrayField
public final <T> JsonWriter writeArrayField(String fieldName, Iterable<T> array, WriteValueCallback<JsonWriter, T> elementWriterFunc) throws IOExceptionWrites 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 (]).The field is only written when
valueisn't null, if a null field needs to be written usewriteNullableField(String, Object, WriteValueCallback).Combines
writeFieldName(String)andwriteArray(Iterable, WriteValueCallback)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.
- Throws:
NullPointerException- IffieldNameorelementWriterFuncis null.IOException- If thefieldNameor the JSON array fails to be written, either JSON start or end array or the element write.
-
writeMapField
public final <T> JsonWriter writeMapField(String fieldName, Map<String, T> map, WriteValueCallback<JsonWriter, throws IOExceptionT> 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 (}).The field is only written when
valueisn't null, if a null field needs to be written usewriteNullableField(String, Object, WriteValueCallback).Combines
writeFieldName(String)andwriteMap(Map, WriteValueCallback)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.
- Throws:
NullPointerException- IffieldNameorvalueWriterFuncis null.IOException- If thefieldNameor the JSON map fails to be written, either JSON start or end object or the key or value write.
-
writeBinaryField
Writes a JSON binary field.Combines
writeFieldName(String)andwriteBinary(byte[])to simplify adding a key-value to a JSON object.The field is only written when
valueisn't null, if a null field needs to be written usewriteNullableField(String, Object, WriteValueCallback).- Parameters:
fieldName- The field name.value- Binary value to write.- Returns:
- The updated JsonWriter object.
- Throws:
NullPointerException- IffieldNameis null.IOException- If either thefieldNameor binaryvaluefails to be written.
-
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.
- Throws:
NullPointerException- IffieldNameis null.IOException- If either thefieldNameor booleanvaluefails to be written.
-
writeBooleanField
Writes a nullable JSON boolean field.Combines
writeFieldName(String)andwriteBoolean(Boolean)to simplify adding a key-value to a JSON object.The field is only written when
valueisn't null, if a null field needs to be written usewriteNullableField(String, Object, WriteValueCallback).- Parameters:
fieldName- The field name.value- Boolean value to write.- Returns:
- The updated JsonWriter object.
- Throws:
NullPointerException- IffieldNameis null.IOException- If either thefieldNameor Booleanvaluefails to be written.
-
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.
- Throws:
NullPointerException- IffieldNameis null.IOException- If either thefieldNameor doublevaluefails to be written.
-
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.
- Throws:
NullPointerException- IffieldNameis null.IOException- If either thefieldNameor floatvaluefails to be written.
-
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.
- Throws:
NullPointerException- IffieldNameis null.IOException- If either thefieldNameor intvaluefails to be written.
-
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.
- Throws:
NullPointerException- IffieldNameis null.IOException- If either thefieldNameor longvaluefails to be written.
-
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.
- Throws:
NullPointerException- IffieldNameis null.IOException- If either thefieldNameor JSON null fails to be written.
-
writeNumberField
Writes a nullable JSON number field.Combines
writeFieldName(String)andwriteNumber(Number)to simplify adding a key-value to a JSON object.The field is only written when
valueisn't null, if a null field needs to be written usewriteNullableField(String, Object, WriteValueCallback).- Parameters:
fieldName- The field name.value- Number value to write.- Returns:
- The updated JsonWriter object.
- Throws:
NullPointerException- IffieldNameis null.IOException- If either thefieldNameor Numbervaluefails to be written.
-
writeStringField
Writes a JSON String field.Combines
writeFieldName(String)andwriteString(String)to simplify adding a key-value to a JSON object.The field is only written when
valueisn't null, if a null field needs to be written usewriteNullableField(String, Object, WriteValueCallback).- Parameters:
fieldName- The field name.value- String value to write.- Returns:
- The updated JsonWriter object.
- Throws:
NullPointerException- IffieldNameis null.IOException- If either thefieldNameor Stringvaluefails to be written.
-
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.
- Throws:
NullPointerException- IffieldNameorvalueis null.IOException- If either thefieldNameor rawvaluefails to be written.
-
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.
- Throws:
NullPointerException- IffieldNameis null.IOException- If eitherfieldNameor the untypedvaluefails to be written.
- 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.
- Throws:
IOException- If the untypedvaluefails to be written.
- null ->
-