Class DefaultJsonWriter
- All Implemented Interfaces:
Closeable,AutoCloseable
JsonWriter implementation.-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the JSON stream.flush()Flushes any un-flushed content written to this writer.Gets the currentwriting contextfor the JSON object.static JsonWritertoStream(OutputStream stream) Creates aDefaultJsonWriterthat 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 (trueorfalse).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 aDefaultJsonWriterthat writes the givenOutputStream.The passed
OutputStreamwon't be closed whenclose()is called as theDefaultJsonWriterisn't the owner of the stream.- Parameters:
stream- TheOutputStreamthat will be written.- Returns:
- An instance of
DefaultJsonWriter. - Throws:
UncheckedIOException- If aDefaultJsonWriterwasn't able to be constructed from theOutputStream.
-
flush
Description copied from class:JsonWriterFlushes any un-flushed content written to this writer.It should be assumed that each write call won't flush any contents.
- Specified by:
flushin classJsonWriter- Returns:
- The flushed JsonWriter object.
-
writeStartObject
Description copied from class:JsonWriterWrites a JSON start object ({).- Specified by:
writeStartObjectin classJsonWriter- Returns:
- The updated JsonWriter object.
-
writeEndObject
Description copied from class:JsonWriterWrites a JSON end object (}).If the current writing context isn't an object an
IllegalStateExceptionwill be thrown.- Specified by:
writeEndObjectin classJsonWriter- Returns:
- The updated JsonWriter object.
-
writeStartArray
Description copied from class:JsonWriterWrites a JSON start array ([).- Specified by:
writeStartArrayin classJsonWriter- Returns:
- The updated JsonWriter object.
-
writeEndArray
Description copied from class:JsonWriterWrites a JSON end array (]).- Specified by:
writeEndArrayin classJsonWriter- Returns:
- The updated JsonWriter object.
-
writeFieldName
Description copied from class:JsonWriterWrites a JSON field name ("fieldName":).- Specified by:
writeFieldNamein classJsonWriter- Parameters:
fieldName- The field name.- Returns:
- The updated JsonWriter object.
-
writeBinary
Description copied from class:JsonWriterWrites 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, 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:
writeBinaryin classJsonWriter- Parameters:
value- Binary value to write.- Returns:
- The updated JsonWriter object.
-
writeBoolean
Description copied from class:JsonWriterWrites a JSON boolean value (trueorfalse).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
BooleanuseJsonWriter.writeBoolean(Boolean).- Specified by:
writeBooleanin classJsonWriter- Parameters:
value- boolean value to write.- Returns:
- The updated JsonWriter object.
-
writeDouble
Description copied from class:JsonWriterWrites 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
DoubleuseJsonWriter.writeDouble(Double).- Specified by:
writeDoublein classJsonWriter- Parameters:
value- double value to write.- Returns:
- The updated JsonWriter object.
-
writeFloat
Description copied from class:JsonWriterWrites 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
FloatuseJsonWriter.writeFloat(Float).- Specified by:
writeFloatin classJsonWriter- Parameters:
value- float value to write.- Returns:
- The updated JsonWriter object.
-
writeInt
Description copied from class:JsonWriterWrites 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
IntegeruseJsonWriter.writeInteger(Integer).- Specified by:
writeIntin classJsonWriter- Parameters:
value- int value to write.- Returns:
- The updated JsonWriter object.
-
writeLong
Description copied from class:JsonWriterWrites 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
LonguseJsonWriter.writeLong(Long).- Specified by:
writeLongin classJsonWriter- Parameters:
value- long value to write.- Returns:
- The updated JsonWriter object.
-
writeNull
Description copied from class:JsonWriterWrites 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:
writeNullin classJsonWriter- Returns:
- The updated JsonWriter object.
-
writeString
Description copied from class:JsonWriterWrites 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, 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:
writeStringin classJsonWriter- Parameters:
value- String value to write.- Returns:
- The updated JsonWriter object.
-
writeRawValue
Description copied from class:JsonWriterWrites 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:
writeRawValuein classJsonWriter- Parameters:
value- The raw JSON value to write.- Returns:
- The updated JsonWriter object.
-
writeBinaryField
Description copied from class:JsonWriterWrites 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
valueis 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:
writeBinaryFieldin classJsonWriter- Parameters:
fieldName- The field name.value- Binary value to write.- Returns:
- The updated JsonWriter object.
-
writeBooleanField
Description copied from class:JsonWriterWrites a JSON boolean field.Combines
JsonWriter.writeFieldName(String)andJsonWriter.writeBoolean(boolean)to simplify adding a key-value to a JSON object.- Specified by:
writeBooleanFieldin classJsonWriter- Parameters:
fieldName- The field name.value- boolean value to write.- Returns:
- The updated JsonWriter object.
-
writeDoubleField
Description copied from class:JsonWriterWrites a JSON double field.Combines
JsonWriter.writeFieldName(String)andJsonWriter.writeDouble(double)to simplify adding a key-value to a JSON object.- Specified by:
writeDoubleFieldin classJsonWriter- Parameters:
fieldName- The field name.value- double value to write.- Returns:
- The updated JsonWriter object.
-
writeFloatField
Description copied from class:JsonWriterWrites a JSON float field.Combines
JsonWriter.writeFieldName(String)andJsonWriter.writeFloat(float)to simplify adding a key-value to a JSON object.- Specified by:
writeFloatFieldin classJsonWriter- Parameters:
fieldName- The field name.value- float value to write.- Returns:
- The updated JsonWriter object.
-
writeIntField
Description copied from class:JsonWriterWrites a JSON int field.Combines
JsonWriter.writeFieldName(String)andJsonWriter.writeInt(int)to simplify adding a key-value to a JSON object.- Specified by:
writeIntFieldin classJsonWriter- Parameters:
fieldName- The field name.value- int value to write.- Returns:
- The updated JsonWriter object.
-
writeLongField
Description copied from class:JsonWriterWrites a JSON long field.Combines
JsonWriter.writeFieldName(String)andJsonWriter.writeLong(long)to simplify adding a key-value to a JSON object.- Specified by:
writeLongFieldin classJsonWriter- Parameters:
fieldName- The field name.value- long value to write.- Returns:
- The updated JsonWriter object.
-
writeNullField
Description copied from class:JsonWriterWrites a JSON null field ("fieldName":null).Combines
JsonWriter.writeFieldName(String)andJsonWriter.writeNull()to simplify adding a key-value to a JSON object.- Specified by:
writeNullFieldin classJsonWriter- Parameters:
fieldName- The field name.- Returns:
- The updated JsonWriter object.
-
writeStringField
Description copied from class:JsonWriterWrites 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
valueis 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:
writeStringFieldin classJsonWriter- Parameters:
fieldName- The field name.value- String value to write.- Returns:
- The updated JsonWriter object.
-
writeRawField
Description copied from class:JsonWriterWrites 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:
writeRawFieldin classJsonWriter- Parameters:
fieldName- The field name.value- The raw JSON value to write.- Returns:
- The updated JsonWriter object.
-
getWriteContext
Description copied from class:JsonWriterGets 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.- Specified by:
getWriteContextin classJsonWriter- Returns:
- The current writing context.
-
close
Description copied from class:JsonWriterCloses the JSON stream.If the
writing contextisn'tJsonWriteContext.COMPLETEDwhen this is called anIllegalStateExceptionwill be thrown.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classJsonWriter- Throws:
IOException
-