Class JsonTokenWriter
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable,RecordWriter,TokenWriter
TokenWriter surface for the JSON format.
Emits RFC-8259-compliant JSON. Output style is driven by a small JsonTokenWriter.Settings bundle that
mirrors the structurally-applicable subset of JsonSerializer.Builder's formatting flags:
JsonSerializer.writeTokens(Object) the writer
inherits the settings from the calling serializer; constructing the writer directly via
JsonTokenWriter(Writer) uses JsonTokenWriter.Settings.DEFAULT (compact, double-quoted, RFC-8259
strict).
Notes:
- This class is not thread safe.
- The writer tracks just enough state to insert structural separators (
, between array/object members and: between field names and values). It does not validate event ordering — emittingfield twice in a row, for instance, throws. - The writer is purely structural; object swaps and
@Schemaannotations have no effect. Use the POJO databind path onJsonSerializerfor those.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordOutput-formatting settings honored byJsonTokenWriter. -
Constructor Summary
ConstructorsConstructorDescriptionJsonTokenWriter(Writer out) Constructor for aWritertarget with defaultJsonTokenWriter.Settings.JsonTokenWriter(Writer out, JsonTokenWriter.Settings settings) Constructor for aWritertarget with explicitJsonTokenWriter.Settings. -
Method Summary
Modifier and TypeMethodDescriptionbinary(byte[] value) Emits aTokenType.VALUE_BINARYevent.bool(boolean value) Emits aTokenType.VALUE_BOOLEANevent.voidclose()Closes the writer, flushing any buffered output first.endArray()Emits aTokenType.END_ARRAYevent.Emits aTokenType.END_OBJECTevent.Emits aTokenType.FIELD_NAMEevent for the next field of the enclosing object.voidflush()Flushes any buffered output to the underlying stream.static JsonTokenWriterforOutput(Object output, JsonTokenWriter.Settings settings) Internal factory used byJsonSerializer.writeTokens(Object).nil()Emits aTokenType.VALUE_NULLevent.number(double value) Emits aTokenType.VALUE_NUMBERevent for adouble.number(long value) Emits aTokenType.VALUE_NUMBERevent for along.Emits aTokenType.VALUE_NUMBERevent for an arbitraryNumber.number(BigDecimal value) Emits aTokenType.VALUE_NUMBERevent for aBigDecimal.number(BigInteger value) Emits aTokenType.VALUE_NUMBERevent for aBigInteger.Emits an arbitrary POJO through the format's structural events.Returns theJsonTokenWriter.Settingsthis writer was configured with.Emits aTokenType.START_ARRAYevent.Emits aTokenType.START_OBJECTevent.Emits aTokenType.VALUE_STRINGevent.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.juneau.marshall.stream.TokenWriter
isStreaming, write, writeExt, writeSimple, writeTag
-
Constructor Details
-
JsonTokenWriter
Constructor for aWritertarget with defaultJsonTokenWriter.Settings.- Parameters:
out- The writer to emit JSON to. Must not benull .
-
JsonTokenWriter
Constructor for aWritertarget with explicitJsonTokenWriter.Settings.- Parameters:
out- The writer to emit JSON to. Must not benull .settings- The output-formatting settings. Must not benull .
-
-
Method Details
-
startObject
Description copied from interface:TokenWriterEmits aTokenType.START_OBJECTevent.- Specified by:
startObjectin interfaceTokenWriter- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.
-
endObject
Description copied from interface:TokenWriterEmits aTokenType.END_OBJECTevent.- Specified by:
endObjectin interfaceTokenWriter- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.
-
startArray
Description copied from interface:TokenWriterEmits aTokenType.START_ARRAYevent.- Specified by:
startArrayin interfaceTokenWriter- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.
-
endArray
Description copied from interface:TokenWriterEmits aTokenType.END_ARRAYevent.- Specified by:
endArrayin interfaceTokenWriter- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.
-
fieldName
Description copied from interface:TokenWriterEmits aTokenType.FIELD_NAMEevent for the next field of the enclosing object.- Specified by:
fieldNamein interfaceTokenWriter- Parameters:
name- The field name. Must not benull .- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.
-
string
Description copied from interface:TokenWriterEmits aTokenType.VALUE_STRINGevent.- Specified by:
stringin interfaceTokenWriter- Parameters:
value- The value. May benull , in which case aTokenType.VALUE_NULLevent is emitted instead.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.
-
number
Description copied from interface:TokenWriterEmits aTokenType.VALUE_NUMBERevent for an arbitraryNumber.- Specified by:
numberin interfaceTokenWriter- Parameters:
value- The value. May benull , in which case aTokenType.VALUE_NULLevent is emitted instead.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.
-
number
Description copied from interface:TokenWriterEmits aTokenType.VALUE_NUMBERevent for along.- Specified by:
numberin interfaceTokenWriter- Parameters:
value- The value.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.
-
number
Description copied from interface:TokenWriterEmits aTokenType.VALUE_NUMBERevent for adouble.- Specified by:
numberin interfaceTokenWriter- Parameters:
value- The value.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.
-
number
Description copied from interface:TokenWriterEmits aTokenType.VALUE_NUMBERevent for aBigDecimal.- Specified by:
numberin interfaceTokenWriter- Parameters:
value- The value. May benull , in which case aTokenType.VALUE_NULLevent is emitted instead.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.
-
number
Description copied from interface:TokenWriterEmits aTokenType.VALUE_NUMBERevent for aBigInteger.- Specified by:
numberin interfaceTokenWriter- Parameters:
value- The value. May benull , in which case aTokenType.VALUE_NULLevent is emitted instead.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.
-
bool
Description copied from interface:TokenWriterEmits aTokenType.VALUE_BOOLEANevent.- Specified by:
boolin interfaceTokenWriter- Parameters:
value- The value.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.
-
nil
Description copied from interface:TokenWriterEmits aTokenType.VALUE_NULLevent.- Specified by:
nilin interfaceTokenWriter- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.
-
object
Description copied from interface:TokenWriterEmits an arbitrary POJO through the format's structural events.The value is walked via
PojoWalker: nulls / scalars / collections / arrays / maps / beans are introspected and re-emitted through this writer'sTokenWriter.startObject(),TokenWriter.fieldName(String),TokenWriter.string(String),TokenWriter.number(long),TokenWriter.bool(boolean),TokenWriter.nil(), andTokenWriter.binary(byte[])methods. Per-format encoding (quoting, indentation, newlines, etc.) is whatever this writer is configured for.Cursor positioning: the writer must be in a state where a value emit is legal — at root, after a
TokenWriter.fieldName(String)call, or inside an array. Callingobject(...)from inside an object body without a preceding field name throwsIllegalStateException, same as callingTokenWriter.string(String)would.Default behavior: the default implementation throws
UnsupportedOperationException. Format-specific token writers override this by delegating toPojoWalker.walk(TokenWriter, Object).Swaps and
@Schema: this method is purely structural. Object swaps and schema annotations are NOT applied at the token layer — use the POJO databind path onSerializer.write(Object)for those.- Specified by:
objectin interfaceTokenWriter- Parameters:
value- The value to emit. May benull .- Returns:
- This object.
- Throws:
IOException- If the underlying stream fails.
-
settingsAccess
Returns theJsonTokenWriter.Settingsthis writer was configured with.Exposed for delegating wrappers (e.g.
JsonlTokenWriter) that need to read the underlying writer's walk options when implementingobject(Object).- Returns:
- The settings. Never
null .
-
binary
Description copied from interface:TokenWriterEmits aTokenType.VALUE_BINARYevent.Text formats normally encode the value as base64-encoded
TokenType.VALUE_STRING; the exact encoding is format-defined.- Specified by:
binaryin interfaceTokenWriter- Parameters:
value- The value. May benull , in which case aTokenType.VALUE_NULLevent is emitted instead.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.
-
flush
Description copied from interface:TokenWriterFlushes any buffered output to the underlying stream.- Specified by:
flushin interfaceFlushable- Specified by:
flushin interfaceRecordWriter- Specified by:
flushin interfaceTokenWriter- Throws:
IOException- If a problem occurred flushing the underlying stream.
-
close
Description copied from interface:TokenWriterCloses the writer, flushing any buffered output first.Implementations should be idempotent — closing a closed writer is a no-op.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceRecordWriter- Specified by:
closein interfaceTokenWriter- Throws:
IOException
-
forOutput
public static JsonTokenWriter forOutput(Object output, JsonTokenWriter.Settings settings) throws IOException Internal factory used byJsonSerializer.writeTokens(Object). Coerces the supported output types (Writer/OutputStream) into aWriterthe JSON encoder can target.- Parameters:
output- The output object.settings- The output-formatting settings.- Returns:
- A new
JsonTokenWriterwriting to the resolvedWriter. - Throws:
IOException- If the output type is not supported or could not be opened.
-