Class DefaultJsonWriter
- All Implemented Interfaces:
Closeable
,AutoCloseable
JsonWriter
implementation.-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the JSON stream.flush()
Flushes any un-flushed content written to this writer.Gets the currentwriting context
for the JSON object.static JsonWriter
toStream
(OutputStream stream) Creates aDefaultJsonWriter
that writes the givenOutputStream
.writeBinary
(byte[] value) Writes a JSON binary value.writeBinaryField
(String fieldName, byte[] value) Writes a JSON binary field.writeBoolean
(boolean value) Writes a JSON boolean value (true
orfalse
).writeBooleanField
(String fieldName, boolean value) Writes a JSON boolean field.writeDouble
(double value) Writes a JSON double value.writeDoubleField
(String fieldName, double value) Writes a JSON double field.Writes a JSON end array (]
).Writes a JSON end object (}
).writeFieldName
(String fieldName) Writes a JSON field name ("fieldName":
).writeFloat
(float value) Writes a JSON float value.writeFloatField
(String fieldName, float value) Writes a JSON float field.writeInt
(int value) Writes a JSON int value.writeIntField
(String fieldName, int value) Writes a JSON int field.writeLong
(long value) Writes a JSON long value.writeLongField
(String fieldName, long value) Writes a JSON long field.Writes a JSON null.writeNullField
(String fieldName) Writes a JSON null field ("fieldName":null
).writeRawField
(String fieldName, String value) Writes the passed field literally without any additional handling.writeRawValue
(String value) Writes the passed value literally without any additional handling.Writes a JSON start array ([
).Writes a JSON start object ({
).writeString
(String value) Writes a JSON String value.writeStringField
(String fieldName, String value) Writes a JSON String field.Methods inherited from class com.azure.json.JsonWriter
writeArray, writeArray, writeArray, writeArray, writeArrayField, writeArrayField, writeArrayField, writeArrayField, writeBinary, writeBinaryField, writeBoolean, writeBoolean, writeBooleanField, writeBooleanField, writeDouble, writeDouble, writeDoubleField, writeDoubleField, writeFloat, writeFloat, writeFloatField, writeFloatField, writeInteger, writeInteger, writeIntegerField, writeIntegerField, writeJson, writeJson, writeJsonField, writeJsonField, writeLong, writeLong, writeLongField, writeLongField, writeMap, writeMap, writeMapField, writeMapField, writeStartArray, writeStartObject, writeString, writeStringField, writeUntyped, writeUntypedField
-
Method Details
-
toStream
Creates aDefaultJsonWriter
that writes the givenOutputStream
.The passed
OutputStream
won't be closed whenclose()
is called as theDefaultJsonWriter
isn't the owner of the stream.- Parameters:
stream
- TheOutputStream
that will be written.- Returns:
- An instance of
DefaultJsonWriter
. - Throws:
UncheckedIOException
- If aDefaultJsonWriter
wasn't able to be constructed from theOutputStream
.
-
flush
Description copied from class:JsonWriter
Flushes any un-flushed content written to this writer.It should be assumed that each write call won't flush any contents.
- Specified by:
flush
in classJsonWriter
- Returns:
- The flushed JsonWriter object.
-
writeStartObject
Description copied from class:JsonWriter
Writes a JSON start object ({
).- Specified by:
writeStartObject
in classJsonWriter
- Returns:
- The updated JsonWriter object.
-
writeEndObject
Description copied from class:JsonWriter
Writes a JSON end object (}
).If the current writing context isn't an object an
IllegalStateException
will be thrown.- Specified by:
writeEndObject
in classJsonWriter
- Returns:
- The updated JsonWriter object.
-
writeStartArray
Description copied from class:JsonWriter
Writes a JSON start array ([
).- Specified by:
writeStartArray
in classJsonWriter
- Returns:
- The updated JsonWriter object.
-
writeEndArray
Description copied from class:JsonWriter
Writes a JSON end array (]
).- Specified by:
writeEndArray
in classJsonWriter
- Returns:
- The updated JsonWriter object.
-
writeFieldName
Description copied from class:JsonWriter
Writes a JSON field name ("fieldName":
).- Specified by:
writeFieldName
in classJsonWriter
- Parameters:
fieldName
- The field name.- Returns:
- The updated JsonWriter object.
-
writeBinary
Description copied from class:JsonWriter
Writes a JSON binary value.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, useJsonWriter.writeBinary(byte[], boolean)
to indicate whether null should be written.This API is used instead of
JsonWriter.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 toJsonWriter.writeFieldName(String)
.- Specified by:
writeBinary
in classJsonWriter
- Parameters:
value
- Binary value to write.- Returns:
- The updated JsonWriter object.
-
writeBoolean
Description copied from class:JsonWriter
Writes a JSON boolean value (true
orfalse
).This API is used instead of
JsonWriter.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 toJsonWriter.writeFieldName(String)
.For the nullable
Boolean
useJsonWriter.writeBoolean(Boolean)
.- Specified by:
writeBoolean
in classJsonWriter
- Parameters:
value
- boolean value to write.- Returns:
- The updated JsonWriter object.
-
writeDouble
Description copied from class:JsonWriter
Writes a JSON double value.This API is used instead of
JsonWriter.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 toJsonWriter.writeFieldName(String)
.For the nullable
Double
useJsonWriter.writeDouble(Double)
.- Specified by:
writeDouble
in classJsonWriter
- Parameters:
value
- double value to write.- Returns:
- The updated JsonWriter object.
-
writeFloat
Description copied from class:JsonWriter
Writes a JSON float value.This API is used instead of
JsonWriter.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 toJsonWriter.writeFieldName(String)
.For the nullable
Float
useJsonWriter.writeFloat(Float)
.- Specified by:
writeFloat
in classJsonWriter
- Parameters:
value
- float value to write.- Returns:
- The updated JsonWriter object.
-
writeInt
Description copied from class:JsonWriter
Writes a JSON int value.This API is used instead of
JsonWriter.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 toJsonWriter.writeFieldName(String)
.For the nullable
Integer
useJsonWriter.writeInteger(Integer)
.- Specified by:
writeInt
in classJsonWriter
- Parameters:
value
- int value to write.- Returns:
- The updated JsonWriter object.
-
writeLong
Description copied from class:JsonWriter
Writes a JSON long value.This API is used instead of
JsonWriter.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 toJsonWriter.writeFieldName(String)
.For the nullable
Long
useJsonWriter.writeLong(Long)
.- Specified by:
writeLong
in classJsonWriter
- Parameters:
value
- long value to write.- Returns:
- The updated JsonWriter object.
-
writeNull
Description copied from class:JsonWriter
Writes a JSON null.This API is used instead of
JsonWriter.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 toJsonWriter.writeFieldName(String)
.- Specified by:
writeNull
in classJsonWriter
- Returns:
- The updated JsonWriter object.
-
writeString
Description copied from class:JsonWriter
Writes a JSON String value.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, useJsonWriter.writeString(String, boolean)
to indicate whether null should be written.This API is used instead of
JsonWriter.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 toJsonWriter.writeFieldName(String)
.- Specified by:
writeString
in classJsonWriter
- Parameters:
value
- String value to write.- Returns:
- The updated JsonWriter object.
-
writeRawValue
Description copied from class:JsonWriter
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
JsonWriter.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 toJsonWriter.writeFieldName(String)
.- Specified by:
writeRawValue
in classJsonWriter
- Parameters:
value
- The raw JSON value to write.- Returns:
- The updated JsonWriter object.
-
writeBinaryField
Description copied from class:JsonWriter
Writes a JSON binary field.Combines
JsonWriter.writeFieldName(String)
andJsonWriter.writeBinary(byte[])
to simplify adding a key-value to a JSON object.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, useJsonWriter.writeBinaryField(String, byte[], boolean)
to indicate whether null should be written.- Specified by:
writeBinaryField
in classJsonWriter
- Parameters:
fieldName
- The field name.value
- Binary value to write.- Returns:
- The updated JsonWriter object.
-
writeBooleanField
Description copied from class:JsonWriter
Writes a JSON boolean field.Combines
JsonWriter.writeFieldName(String)
andJsonWriter.writeBoolean(boolean)
to simplify adding a key-value to a JSON object.- Specified by:
writeBooleanField
in classJsonWriter
- Parameters:
fieldName
- The field name.value
- boolean value to write.- Returns:
- The updated JsonWriter object.
-
writeDoubleField
Description copied from class:JsonWriter
Writes a JSON double field.Combines
JsonWriter.writeFieldName(String)
andJsonWriter.writeDouble(double)
to simplify adding a key-value to a JSON object.- Specified by:
writeDoubleField
in classJsonWriter
- Parameters:
fieldName
- The field name.value
- double value to write.- Returns:
- The updated JsonWriter object.
-
writeFloatField
Description copied from class:JsonWriter
Writes a JSON float field.Combines
JsonWriter.writeFieldName(String)
andJsonWriter.writeFloat(float)
to simplify adding a key-value to a JSON object.- Specified by:
writeFloatField
in classJsonWriter
- Parameters:
fieldName
- The field name.value
- float value to write.- Returns:
- The updated JsonWriter object.
-
writeIntField
Description copied from class:JsonWriter
Writes a JSON int field.Combines
JsonWriter.writeFieldName(String)
andJsonWriter.writeInt(int)
to simplify adding a key-value to a JSON object.- Specified by:
writeIntField
in classJsonWriter
- Parameters:
fieldName
- The field name.value
- int value to write.- Returns:
- The updated JsonWriter object.
-
writeLongField
Description copied from class:JsonWriter
Writes a JSON long field.Combines
JsonWriter.writeFieldName(String)
andJsonWriter.writeLong(long)
to simplify adding a key-value to a JSON object.- Specified by:
writeLongField
in classJsonWriter
- Parameters:
fieldName
- The field name.value
- long value to write.- Returns:
- The updated JsonWriter object.
-
writeNullField
Description copied from class:JsonWriter
Writes a JSON null field ("fieldName":null
).Combines
JsonWriter.writeFieldName(String)
andJsonWriter.writeNull()
to simplify adding a key-value to a JSON object.- Specified by:
writeNullField
in classJsonWriter
- Parameters:
fieldName
- The field name.- Returns:
- The updated JsonWriter object.
-
writeStringField
Description copied from class:JsonWriter
Writes a JSON String field.Combines
JsonWriter.writeFieldName(String)
andJsonWriter.writeString(String)
to simplify adding a key-value to a JSON object.A value is always written no matter whether
value
is null, if null shouldn't be written this API call must be null guarded. Or, useJsonWriter.writeStringField(String, String, boolean)
to indicate whether null should be written.- Specified by:
writeStringField
in classJsonWriter
- Parameters:
fieldName
- The field name.value
- String value to write.- Returns:
- The updated JsonWriter object.
-
writeRawField
Description copied from class:JsonWriter
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
JsonWriter.writeFieldName(String)
andJsonWriter.writeRawValue(String)
to simplify adding a key-value to a JSON object.- Specified by:
writeRawField
in classJsonWriter
- Parameters:
fieldName
- The field name.value
- The raw JSON value to write.- Returns:
- The updated JsonWriter object.
-
getWriteContext
Description copied from class:JsonWriter
Gets the currentwriting context
for the JSON object.The writing context can help determine whether a write operation would be illegal.
The initial write context is
JsonWriteContext.ROOT
.- Specified by:
getWriteContext
in classJsonWriter
- Returns:
- The current writing context.
-
close
Description copied from class:JsonWriter
Closes the JSON stream.If the
writing context
isn'tJsonWriteContext.COMPLETED
when this is called anIllegalStateException
will be thrown.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classJsonWriter
- Throws:
IOException
-