JsonWriter Class

  • java.lang.Object
    • com.azure.json.JsonWriter

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

Modifier and Type Method and Description
final JsonWriter writeArray(T[] array, WriteValueCallback<JsonWriter,T> elementWriterFunc)

Writes a JSON array.

final JsonWriter writeArray(Iterable<T> array, WriteValueCallback<JsonWriter,T> elementWriterFunc)

Writes a JSON array.

final JsonWriter writeArrayField(String fieldName, T[] array, WriteValueCallback<JsonWriter,T> elementWriterFunc)

Writes a JSON array field.

final JsonWriter writeArrayField(String fieldName, Iterable<T> array, WriteValueCallback<JsonWriter,T> elementWriterFunc)

Writes a JSON array field.

final JsonWriter writeMap(Map<String,T> map, WriteValueCallback<JsonWriter,T> valueWriterFunc)

Writes a JSON map.

final JsonWriter writeMapField(String fieldName, Map<String,T> map, WriteValueCallback<JsonWriter,T> valueWriterFunc)

Writes a JSON map field.

final JsonWriter writeNullableField(String fieldName, T nullable, WriteValueCallback<JsonWriter,T> writerFunc)

Writes a nullable JSON field.

abstract void close()

Closes the JSON stream.

abstract JsonWriter flush()

Flushes any un-flushed content written to this writer.

abstract JsonWriteContext getWriteContext()

Gets the current JsonWriteContext for the JSON object.

abstract JsonWriter writeBinary(byte[] value)

Writes a JSON binary value.

final JsonWriter writeBinaryField(String fieldName, byte[] value)

Writes a JSON binary field.

abstract JsonWriter writeBoolean(boolean value)

Writes a JSON boolean value (true or false).

final JsonWriter writeBoolean(Boolean value)

Writes a nullable JSON boolean value (true, false, or null).

final JsonWriter writeBooleanField(String fieldName, boolean value)

Writes a JSON boolean field.

final JsonWriter writeBooleanField(String fieldName, Boolean value)

Writes a nullable JSON boolean field.

abstract JsonWriter writeDouble(double value)

Writes a JSON double value.

final JsonWriter writeDoubleField(String fieldName, double value)

Writes a JSON double field.

abstract JsonWriter writeEndArray()

Writes a JSON end array (]).

abstract JsonWriter writeEndObject()

Writes a JSON end object (}).

abstract JsonWriter writeFieldName(String fieldName)

Writes a JSON field name ("fieldName":).

abstract JsonWriter writeFloat(float value)

Writes a JSON float value.

final JsonWriter writeFloatField(String fieldName, float value)

Writes a JSON float field.

abstract JsonWriter writeInt(int value)

Writes a JSON int value.

final JsonWriter writeIntField(String fieldName, int value)

Writes a JSON int field.

final JsonWriter writeJson(JsonSerializable<?> value)

Writes a JsonSerializable<T> object.

final JsonWriter writeJsonField(String fieldName, JsonSerializable<?> value)

Writes a JsonSerializable<T> field.

abstract JsonWriter writeLong(long value)

Writes a JSON long value.

final JsonWriter writeLongField(String fieldName, long value)

Writes a JSON long field.

abstract JsonWriter writeNull()

Writes a JSON null.

final JsonWriter writeNullField(String fieldName)

Writes a JSON null field ("fieldName":null).

final JsonWriter writeNumber(Number value)

Writes a nullable JSON number value.

final JsonWriter writeNumberField(String fieldName, Number value)

Writes a nullable JSON number field.

final JsonWriter writeRawField(String fieldName, String value)

Writes the passed field literally without any additional handling.

abstract JsonWriter writeRawValue(String value)

Writes the passed value literally without any additional handling.

abstract JsonWriter writeStartArray()

Writes a JSON start array ([).

final JsonWriter writeStartArray(String fieldName)

Writes a JSON start array ([) with a preceding field name.

abstract JsonWriter writeStartObject()

Writes a JSON start object ({).

final JsonWriter writeStartObject(String fieldName)

Writes a JSON start object ({) with a preceding field name.

abstract JsonWriter writeString(String value)

Writes a JSON String value.

final JsonWriter writeStringField(String fieldName, String value)

Writes a JSON String field.

JsonWriter writeUntyped(Object value)

Writes the unknown type value.

JsonWriter writeUntypedField(String fieldName, Object value)

Writes the unknown type value field.

Methods inherited from java.lang.Object

Constructor Details

JsonWriter

public JsonWriter()

Creates an instance of JsonWriter.

Method Details

writeArray

public final JsonWriter writeArray(T[] array, WriteValueCallback elementWriterFunc)

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 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:

array - The array being written.
elementWriterFunc - The function that writes each element of the array.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If elementWriterFunc is null.

writeArray

public final JsonWriter writeArray(Iterable array, WriteValueCallback elementWriterFunc)

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 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:

array - The array being written.
elementWriterFunc - The function that writes each element of the array.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If elementWriterFunc is null.

writeArrayField

public final JsonWriter writeArrayField(String fieldName, T[] array, WriteValueCallback 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 the elementWriterFunc and finishing by writing the end array (]).

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:

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:

IOException

- If fieldName or elementWriterFunc is null.

writeArrayField

public final JsonWriter writeArrayField(String fieldName, Iterable array, WriteValueCallback 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 the elementWriterFunc and finishing by writing the end array (]).

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:

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:

IOException

- If fieldName or elementWriterFunc is null.

writeMap

public final JsonWriter writeMap(Map map, WriteValueCallback valueWriterFunc)

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 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:

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:

IOException

- If valueWriterFunc is null.

writeMapField

public final JsonWriter writeMapField(String fieldName, Map map, WriteValueCallback 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 the valueWriterFunc and finishing by writing the end object (}).

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:

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:

IOException

- If fieldName or valueWriterFunc is null.

writeNullableField

public final JsonWriter writeNullableField(String fieldName, T nullable, WriteValueCallback writerFunc)

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:

fieldName - The field name.
nullable - The nullable JSON value.
writerFunc - The non-null JSON value writer function.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If fieldName or writerFunc is null.

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:

IOException

- If the JsonWriter is closed before the writing context is JsonWriteContext#COMPLETED.

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:

The flushed JsonWriter object.

Throws:

IOException

- If the underlying content fails to flush.

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:

The current writing context.

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:

value - Binary value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If the binary 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:

fieldName - The field name.
value - Binary value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If 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:

value - boolean value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If the boolean 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:

value - Boolean value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If the Boolean 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:

fieldName - The field name.
value - boolean value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If 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:

fieldName - The field name.
value - Boolean value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If 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:

value - double value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If the double 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:

fieldName - The field name.
value - double value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If fieldName is null.

writeEndArray

public abstract JsonWriter writeEndArray()

Writes a JSON end array (]).

Returns:

The updated JsonWriter object.

Throws:

IOException

- If JSON end array fails to be written.

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:

The updated JsonWriter object.

Throws:

IOException

- If JSON end object fails to be written.

writeFieldName

public abstract JsonWriter writeFieldName(String fieldName)

Writes a JSON field name ("fieldName":).

Parameters:

fieldName - The field name.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If 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:

value - float value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If the float 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:

fieldName - The field name.
value - float value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If 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:

value - int value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If the int 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:

fieldName - The field name.
value - int value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If 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:

value - JsonSerializable<T> object to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If the JsonSerializable fails to be written.

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:

fieldName - The field name.
value - JsonSerializable<T> object to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If 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:

value - long value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If the long 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:

fieldName - The field name.
value - long value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If 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:

The updated JsonWriter object.

Throws:

IOException

- If a JSON null fails to be written.

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:

fieldName - The field name.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If 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:

value - Number value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If the Number 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:

fieldName - The field name.
value - Number value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If 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:

fieldName - The field name.
value - The raw JSON value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If fieldName or value is null.

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:

value - The raw JSON value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If value is null.

writeStartArray

public abstract JsonWriter writeStartArray()

Writes a JSON start array ([).

Returns:

The updated JsonWriter object.

Throws:

IOException

- If JSON start array fails to be written.

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:

fieldName - The field name.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If fieldName is null.

writeStartObject

public abstract JsonWriter writeStartObject()

Writes a JSON start object ({).

Returns:

The updated JsonWriter object.

Throws:

IOException

- If JSON start object fails to be written.

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:

fieldName - The field name.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If 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:

value - String value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If the String 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:

fieldName - The field name.
value - String value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If 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):

Parameters:

value - The value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If the untyped 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):

Parameters:

fieldName - The field name.
value - The value to write.

Returns:

The updated JsonWriter object.

Throws:

IOException

- If fieldName is null.

Applies to