JsonWriter Class
- java.
lang. Object - com.
azure. json. JsonWriter
- com.
Implements
public abstract class JsonWriter
implements Closeable
Writes a JSON value as a stream of tokens.
Instances of JsonWriter are created using an instance of JsonProvider or using the utility methods in JsonProviders.
Constructor Summary
Constructor | Description |
---|---|
JsonWriter() |
Creates an instance of JsonWriter. |
Method Summary
Methods inherited from java.lang.Object
Constructor Details
JsonWriter
public JsonWriter()
Creates an instance of JsonWriter.
Method Details
writeArray
public final JsonWriter
Writes a JSON array.
This API will begin by writing the start array ([
) followed by all elements in the array using the elementWriterFunc
and finishing by writing the end array (]
).
If an element in the array is null, NULL will be written for that element. If null elements should be excluded from serialization use #writeArray(Object[], WriteValueCallback, boolean) and pass true for skipNullElements
.
If array
is null NULL will 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 to writeFieldName(String fieldName).
Parameters:
Returns:
Throws:
elementWriterFunc
is null.
writeArray
public JsonWriter
Writes a JSON array.
This API will begin by writing the start array ([
) followed by all elements in the array using the elementWriterFunc
and finishing by writing the end array (]
).
If an element in the array is null, NULL will be written for that element if skipNullElements
is false. If skipNullElements
is true, null elements will be excluded from serialization.
If array
is null NULL will be written.
This API is used instead of #writeArrayField(String, Object[], WriteValueCallback, 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 to writeFieldName(String fieldName).
Parameters:
Returns:
Throws:
elementWriterFunc
is null.
writeArray
public final JsonWriter
Writes a JSON array.
This API will begin by writing the start array ([
) followed by all elements in the array using the elementWriterFunc
and finishing by writing the end array (]
).
If an element in the array is null, NULL will be written for that element. If null elements should be excluded from serialization use writeArray(Iterable<T> array, WriteValueCallback<JsonWriter,T> elementWriterFunc, boolean skipNullElements) and pass true for skipNullElements
.
If array
is null NULL will be written.
This API is used instead of writeArrayField(String fieldName, Iterable<T> array, WriteValueCallback<JsonWriter,T> elementWriterFunc) when the value needs to be written to the root of the JSON value, as an element in an array, or after a call to writeFieldName(String fieldName).
Parameters:
Returns:
Throws:
elementWriterFunc
is null.
writeArray
public JsonWriter
Writes a JSON array.
This API will begin by writing the start array ([
) followed by all elements in the array using the elementWriterFunc
and finishing by writing the end array (]
).
If an element in the array is null, NULL will be written for that element if skipNullElements
is false. If skipNullElements
is true, null elements will be excluded from serialization.
If array
is null NULL will be written.
This API is used instead of writeArrayField(String fieldName, Iterable<T> array, WriteValueCallback<JsonWriter,T> elementWriterFunc, boolean skipNullElements) when the value needs to be written to the root of the JSON value, as an element in an array, or after a call to writeFieldName(String fieldName).
Parameters:
Returns:
Throws:
elementWriterFunc
is null.
writeArrayField
public final JsonWriter
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 the elementWriterFunc
and finishing by writing the end array (]
).
If an element in the array is null, NULL will be written for that element. If null elements should be excluded from serialization use #writeArrayField(String, Object[], WriteValueCallback, boolean) and pass true for skipNullElements
.
The field is only written when value
isn't null, if a null field needs to be written use #writeNullableField(String, Object, WriteValueCallback).
Combines writeFieldName(String fieldName) and #writeArray(Object[], WriteValueCallback) to simplify adding a key-value to a JSON object.
Parameters:
Returns:
Throws:
writeArrayField
public JsonWriter
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 the elementWriterFunc
and finishing by writing the end array (]
).
If an element in the array is null, NULL will be written for that element if skipNullElements
is false. If skipNullElements
is true, null elements will be excluded from serialization.
The field is only written when value
isn't null, if a null field needs to be written use #writeNullableField(String, Object, WriteValueCallback).
Combines writeFieldName(String fieldName) and #writeArray(Object[], WriteValueCallback, boolean) to simplify adding a key-value to a JSON object.
Parameters:
Returns:
Throws:
writeArrayField
public final JsonWriter
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 the elementWriterFunc
and finishing by writing the end array (]
).
If an element in the array is null, NULL will be written for that element. If null elements should be excluded from serialization use writeArrayField(String fieldName, Iterable<T> array, WriteValueCallback<JsonWriter,T> elementWriterFunc, boolean skipNullElements) and pass true for skipNullElements
.
The field is only written when value
isn't null, if a null field needs to be written use #writeNullableField(String, Object, WriteValueCallback).
Combines writeFieldName(String fieldName) and writeArray(Iterable<T> array, WriteValueCallback<JsonWriter,T> elementWriterFunc) to simplify adding a key-value to a JSON object.
Parameters:
Returns:
Throws:
writeArrayField
public JsonWriter
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 the elementWriterFunc
and finishing by writing the end array (]
).
If an element in the array is null, NULL will be written for that element if skipNullElements
is false. If skipNullElements
is true, null elements will be excluded from serialization.
The field is only written when value
isn't null, if a null field needs to be written use #writeNullableField(String, Object, WriteValueCallback).
Combines writeFieldName(String fieldName) and writeArray(Iterable<T> array, WriteValueCallback<JsonWriter,T> elementWriterFunc, boolean skipNullElements) to simplify adding a key-value to a JSON object.
Parameters:
Returns:
Throws:
writeMap
public final JsonWriter
Writes a JSON map.
This API will begin by writing the start object ({
) followed by key-value fields in the map using the valueWriterFunc
and finishing by writing the end object (}
).
If any value in the map is null and the valueWriterFunc
skips null values, such as writeJson(JsonSerializable<?> value), NULL will be written for that value. Otherwise, handling of the null value is determined by valueWriterFunc
.
If map
is null NULL will be written.
This API is used instead of writeMapField(String fieldName, Map<String,T> map, WriteValueCallback<JsonWriter,T> valueWriterFunc) when the value needs to be written to the root of the JSON value, as an element in an array, or after a call to writeFieldName(String fieldName).
Parameters:
Returns:
Throws:
valueWriterFunc
is null or any key in the map is null.
writeMap
public JsonWriter
Writes a JSON map.
This API will begin by writing the start object ({
) followed by key-value fields in the map using the valueWriterFunc
and finishing by writing the end object (}
).
If a value in the map is null and skipNullValues
is true, that key-value pair will be skipped. Otherwise, handling of the null value will be based on valueWriterFunc
. If valueWriterFunc
skips null values, such as writeJson(JsonSerializable<?> value), NULL will be written for that value.
If map
is null NULL will be written.
This API is used instead of writeMapField(String fieldName, Map<String,T> map, WriteValueCallback<JsonWriter,T> valueWriterFunc, boolean skipNullValues) when the value needs to be written to the root of the JSON value, as an element in an array, or after a call to writeFieldName(String fieldName).
Parameters:
Returns:
Throws:
valueWriterFunc
is null or any key in the map is null.
writeMapField
public final JsonWriter
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 the valueWriterFunc
and finishing by writing the end object (}
).
If any value in the map is null and the valueWriterFunc
skips null values, such as writeJson(JsonSerializable<?> value), NULL will be written for that value. Otherwise, handling of the null value is determined by valueWriterFunc
.
The field is only written when value
isn't null, if a null field needs to be written use #writeNullableField(String, Object, WriteValueCallback).
Combines writeFieldName(String fieldName) and writeMap(Map<String,T> map, WriteValueCallback<JsonWriter,T> valueWriterFunc) to simplify adding a key-value to a JSON object.
Parameters:
Returns:
Throws:
writeMapField
public JsonWriter
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 the valueWriterFunc
and finishing by writing the end object (}
).
If a value in the map is null and skipNullValues
is true, that key-value pair will be skipped. Otherwise, handling of the null value will be based on valueWriterFunc
. If valueWriterFunc
skips null values, such as writeJson(JsonSerializable<?> value), NULL will be written for that value.
The field is only written when value
isn't null, if a null field needs to be written use #writeNullableField(String, Object, WriteValueCallback).
Combines writeFieldName(String fieldName) and writeMap(Map<String,T> map, WriteValueCallback<JsonWriter,T> valueWriterFunc, boolean skipNullValues) to simplify adding a key-value to a JSON object.
Parameters:
Returns:
Throws:
writeNullableField
public final JsonWriter
Writes a nullable JSON field.
When the value
is null this effectively is the same as writeNullField(String fieldName). When the value
isn't null this will write the JSON field name and call the writerFunc
that is supplied with the non-null nullable value and this JsonWriter to perform the write operation.
Parameters:
Returns:
Throws:
close
public abstract void close()
Closes the JSON stream.
If the getWriteContext() isn't COMPLETED when this is called an IllegalStateException will be thrown.
During closing the implementation of JsonWriter must flush any un-flushed content.
Throws:
flush
public abstract JsonWriter flush()
Flushes any un-flushed content written to this writer.
It should be assumed that each write call won't flush any contents.
Returns:
Throws:
getWriteContext
public abstract JsonWriteContext getWriteContext()
Gets the current JsonWriteContext for the JSON object.
The writing context can help determine whether a write operation would be illegal.
The initial write context is ROOT.
Returns:
writeBinary
public abstract JsonWriter writeBinary(byte[] value)
Writes a JSON binary value.
If value
is null NULL will be written.
This API is used instead of writeBinaryField(String fieldName, byte[] value) when the value needs to be written to the root of the JSON value, as an element in an array, or after a call to writeFieldName(String fieldName).
Parameters:
Returns:
Throws:
value
fails to be written.
writeBinaryField
public final JsonWriter writeBinaryField(String fieldName, byte[] value)
Writes a JSON binary field.
Combines writeFieldName(String fieldName) and writeBinary(byte[] value) to simplify adding a key-value to a JSON object.
The field is only written when value
isn't null, if a null field needs to be written use #writeNullableField(String, Object, WriteValueCallback).
Parameters:
Returns:
Throws:
fieldName
is null.
writeBoolean
public abstract JsonWriter writeBoolean(boolean value)
Writes a JSON boolean value (true
or false
).
This API is used instead of writeBooleanField(String fieldName, boolean value) when the value needs to be written to the root of the JSON value, as an element in an array, or after a call to writeFieldName(String fieldName).
For the nullable Boolean
use writeBoolean(Boolean value).
Parameters:
Returns:
Throws:
value
fails to be written.
writeBoolean
public final JsonWriter writeBoolean(Boolean value)
Writes a nullable JSON boolean value (true
, false
, or null
).
If value
is null NULL will be written.
This API is used instead of writeBooleanField(String fieldName, Boolean value) when the value needs to be written to the root of the JSON value, as an element in an array, or after a call to writeFieldName(String fieldName).
For the primitive boolean
use writeBoolean(boolean value).
Parameters:
Returns:
Throws:
value
fails to be written.
writeBooleanField
public final JsonWriter writeBooleanField(String fieldName, boolean value)
Writes a JSON boolean field.
Combines writeFieldName(String fieldName) and writeBoolean(boolean value) to simplify adding a key-value to a JSON object.
Parameters:
Returns:
Throws:
fieldName
is null.
writeBooleanField
public final JsonWriter writeBooleanField(String fieldName, Boolean value)
Writes a nullable JSON boolean field.
Combines writeFieldName(String fieldName) and writeBoolean(Boolean value) to simplify adding a key-value to a JSON object.
The field is only written when value
isn't null, if a null field needs to be written use #writeNullableField(String, Object, WriteValueCallback).
Parameters:
Returns:
Throws:
fieldName
is null.
writeDouble
public abstract JsonWriter writeDouble(double value)
Writes a JSON double value.
This API is used instead of writeDoubleField(String fieldName, double value) when the value needs to be written to the root of the JSON value, as an element in an array, or after a call to writeFieldName(String fieldName).
For the nullable Double
use writeNumber(Number value).
Parameters:
Returns:
Throws:
value
fails to be written.
writeDoubleField
public final JsonWriter writeDoubleField(String fieldName, double value)
Writes a JSON double field.
Combines writeFieldName(String fieldName) and writeDouble(double value) to simplify adding a key-value to a JSON object.
Parameters:
Returns:
Throws:
fieldName
is null.
writeEndArray
public abstract JsonWriter writeEndArray()
Writes a JSON end array (]
).
Returns:
Throws:
writeEndObject
public abstract JsonWriter writeEndObject()
Writes a JSON end object (}
).
If the current writing context isn't an object an IllegalStateException will be thrown.
Returns:
Throws:
writeFieldName
public abstract JsonWriter writeFieldName(String fieldName)
Writes a JSON field name ("fieldName":
).
Parameters:
Returns:
Throws:
fieldName
is null.
writeFloat
public abstract JsonWriter writeFloat(float value)
Writes a JSON float value.
This API is used instead of writeFloatField(String fieldName, float value) when the value needs to be written to the root of the JSON value, as an element in an array, or after a call to writeFieldName(String fieldName).
For the nullable Float
use writeNumber(Number value).
Parameters:
Returns:
Throws:
value
fails to be written.
writeFloatField
public final JsonWriter writeFloatField(String fieldName, float value)
Writes a JSON float field.
Combines writeFieldName(String fieldName) and writeFloat(float value) to simplify adding a key-value to a JSON object.
Parameters:
Returns:
Throws:
fieldName
is null.
writeInt
public abstract JsonWriter writeInt(int value)
Writes a JSON int value.
This API is used instead of writeIntField(String fieldName, int value) when the value needs to be written to the root of the JSON value, as an element in an array, or after a call to writeFieldName(String fieldName).
For the nullable Integer
use writeNumber(Number value).
Parameters:
Returns:
Throws:
value
fails to be written.
writeIntField
public final JsonWriter writeIntField(String fieldName, int value)
Writes a JSON int field.
Combines writeFieldName(String fieldName) and writeInt(int value) to simplify adding a key-value to a JSON object.
Parameters:
Returns:
Throws:
fieldName
is null.
writeJson
public final JsonWriter writeJson(JsonSerializable value)
Writes a JsonSerializable<T> object.
If value
is null NULL will be written.
This API is used instead of writeJsonField(String fieldName, JsonSerializable<?> value) when the value needs to be written to the root of the JSON value, as an element in an array, or after a call to writeFieldName(String fieldName).
Parameters:
Returns:
Throws:
writeJsonField
public final JsonWriter writeJsonField(String fieldName, JsonSerializable value)
Writes a JsonSerializable<T> field.
Combines writeFieldName(String fieldName) and writeJson(JsonSerializable<?> value) to simplify adding a key-value to a JSON object.
The field is only written when value
isn't null, if a null field needs to be written use #writeNullableField(String, Object, WriteValueCallback).
Parameters:
Returns:
Throws:
fieldName
is null.
writeLong
public abstract JsonWriter writeLong(long value)
Writes a JSON long value.
This API is used instead of writeLongField(String fieldName, long value) when the value needs to be written to the root of the JSON value, as an element in an array, or after a call to writeFieldName(String fieldName).
For the nullable Long
use writeNumber(Number value).
Parameters:
Returns:
Throws:
value
fails to be written.
writeLongField
public final JsonWriter writeLongField(String fieldName, long value)
Writes a JSON long field.
Combines writeFieldName(String fieldName) and writeLong(long value) to simplify adding a key-value to a JSON object.
Parameters:
Returns:
Throws:
fieldName
is null.
writeNull
public abstract JsonWriter writeNull()
Writes a JSON null.
This API is used instead of writeNullField(String fieldName) when the value needs to be written to the root of the JSON value, as an element in an array, or after a call to writeFieldName(String fieldName).
Returns:
Throws:
writeNullField
public final JsonWriter writeNullField(String fieldName)
Writes a JSON null field ("fieldName":null
).
Combines writeFieldName(String fieldName) and writeNull() to simplify adding a key-value to a JSON object.
Parameters:
Returns:
Throws:
fieldName
is null.
writeNumber
public final JsonWriter writeNumber(Number value)
Writes a nullable JSON number value.
If value
is null NULL will be written.
This API is used instead of writeNumberField(String fieldName, Number value) when the value needs to be written to the root of the JSON value, as an element in an array, or after a call to writeFieldName(String fieldName).
Parameters:
Returns:
Throws:
value
fails to be written.
writeNumberField
public final JsonWriter writeNumberField(String fieldName, Number value)
Writes a nullable JSON number field.
Combines writeFieldName(String fieldName) and writeNumber(Number value) to simplify adding a key-value to a JSON object.
The field is only written when value
isn't null, if a null field needs to be written use #writeNullableField(String, Object, WriteValueCallback).
Parameters:
Returns:
Throws:
fieldName
is null.
writeRawField
public final JsonWriter writeRawField(String fieldName, String value)
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 fieldName) and writeRawValue(String value) to simplify adding a key-value to a JSON object.
Parameters:
Returns:
Throws:
writeRawValue
public abstract JsonWriter writeRawValue(String value)
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 fieldName, String value) when the value needs to be written to the root of the JSON value, as an element in an array, or after a call to writeFieldName(String fieldName).
Parameters:
Returns:
Throws:
value
is null.
writeStartArray
public abstract JsonWriter writeStartArray()
Writes a JSON start array ([
).
Returns:
Throws:
writeStartArray
public final JsonWriter writeStartArray(String fieldName)
Writes a JSON start array ([
) with a preceding field name.
This API is the equivalent of calling writeFieldName(String fieldName) and writeStartArray(), in that order.
Parameters:
Returns:
Throws:
fieldName
is null.
writeStartObject
public abstract JsonWriter writeStartObject()
Writes a JSON start object ({
).
Returns:
Throws:
writeStartObject
public final JsonWriter writeStartObject(String fieldName)
Writes a JSON start object ({
) with a preceding field name.
This API is the equivalent of calling writeFieldName(String fieldName) and writeStartObject(), in that order.
Parameters:
Returns:
Throws:
fieldName
is null.
writeString
public abstract JsonWriter writeString(String value)
Writes a JSON String value.
If value
is null NULL will be written.
This API is used instead of writeStringField(String fieldName, String value) when the value needs to be written to the root of the JSON value, as an element in an array, or after a call to writeFieldName(String fieldName).
Parameters:
Returns:
Throws:
value
fails to be written.
writeStringField
public final JsonWriter writeStringField(String fieldName, String value)
Writes a JSON String field.
Combines writeFieldName(String fieldName) and writeString(String value) to simplify adding a key-value to a JSON object.
The field is only written when value
isn't null, if a null field needs to be written use #writeNullableField(String, Object, WriteValueCallback).
Parameters:
Returns:
Throws:
fieldName
is null.
writeUntyped
public JsonWriter writeUntyped(Object value)
Writes the unknown type value
.
The following is how each value
is handled (in this order):
- null -> writeNull()
short
-> writeInt(int value)int
-> writeInt(int value)long
-> writeLong(long value)float
-> writeFloat(float value)double
-> writeDouble(double value)boolean
-> writeBoolean(boolean value)- CharSequence -> writeString(String value)
char
-> writeString(String value)- JsonSerializable<T> -> writeJson(JsonSerializable<?> value)
Object[]
-> writeUntyped(Object value) for each element- Iterable -> writeUntyped(Object value) for each element
- Map -> writeUntyped(Object value) for each element where the key to
toString
'd - Object -> empty JSON object (
{}
) - All other values use the
toString
value with writeString(String value)
Parameters:
Returns:
Throws:
value
fails to be written.
writeUntypedField
public JsonWriter writeUntypedField(String fieldName, Object value)
Writes the unknown type value
field.
The following is how each value
is handled (in this order):
- null -> writeNull()
short
-> writeInt(int value)int
-> writeInt(int value)long
-> writeLong(long value)float
-> writeFloat(float value)double
-> writeDouble(double value)boolean
-> writeBoolean(boolean value)- CharSequence -> writeString(String value)
char
-> writeString(String value)- JsonSerializable<T> -> writeJson(JsonSerializable<?> value)
Object[]
-> writeUntyped(Object value) for each element- Iterable -> writeUntyped(Object value) for each element
- Map -> writeUntyped(Object value) for each element where the key to
toString
'd - Object -> empty JSON object (
{}
) - All other values use the
toString
value with writeString(String value)
Parameters:
Returns:
Throws:
fieldName
is null.
Applies to
Azure SDK for Java