Interface TokenWriter
- All Superinterfaces:
AutoCloseable,Closeable,Flushable,RecordWriter
- All Known Implementing Classes:
CborTokenWriter,JsonlTokenWriter,JsonTokenWriter,MsgPackTokenWriter
The caller emits structural events (startObject() / fieldName(String) /
number(long) / ...) and the implementation produces the corresponding bytes for its
format. This is a format-neutral surface; the same writer methods work across every Juneau
format that implements TokenWritable (the structural-token role).
Usage:
StringBuilder
Contract:
- The writer is purely structural. It bypasses object swaps and
@Schemaannotations — that is the role of the POJO databind path onSerializerand its bridges. - It is the caller's responsibility to emit a well-formed event sequence (every
startObject()matched by anendObject(), every field name followed by exactly one value or container, etc.). Implementations are not required to validate the sequence, though they may. - Concrete implementations are not required to be thread-safe.
See Also:
TokenReader— the pull-side counterpart.TokenType— the format-neutral token vocabulary.- Serializers and Parsers
-
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.default booleanStructural token cursors are always O(1)-memory streamers.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.default TokenWriterEmits an arbitrary POJO through the format's structural events.Emits aTokenType.START_ARRAYevent.Emits aTokenType.START_OBJECTevent.Emits aTokenType.VALUE_STRINGevent.default TokenWriterImplementsRecordWriter.write(Object)by delegating toobject(Object).default TokenWriterwriteExt(int type, byte[] payload) Emits a typed-binary value (signed type byte + binary payload).default TokenWriterwriteSimple(int value) Emits an opaque simple value.default TokenWriterwriteTag(long tagNumber) Emits a semantic tag prefixing the next value emit.
-
Method Details
-
startObject
Emits aTokenType.START_OBJECTevent.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.SerializeException- If the format cannot represent this event in the current state.
-
endObject
Emits aTokenType.END_OBJECTevent.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.SerializeException- If the format cannot represent this event in the current state.
-
startArray
Emits aTokenType.START_ARRAYevent.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.SerializeException- If the format cannot represent this event in the current state.
-
endArray
Emits aTokenType.END_ARRAYevent.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.SerializeException- If the format cannot represent this event in the current state.
-
fieldName
Emits aTokenType.FIELD_NAMEevent for the next field of the enclosing object.- Parameters:
name- The field name. Must not benull .- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.SerializeException- If the format cannot represent this event in the current state.
-
string
Emits aTokenType.VALUE_STRINGevent.- 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.SerializeException- If the format cannot represent this event in the current state.
-
number
Emits aTokenType.VALUE_NUMBERevent for an arbitraryNumber.- 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.SerializeException- If the format cannot represent this event in the current state.
-
number
Emits aTokenType.VALUE_NUMBERevent for along.- Parameters:
value- The value.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.SerializeException- If the format cannot represent this event in the current state.
-
number
Emits aTokenType.VALUE_NUMBERevent for adouble.- Parameters:
value- The value.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.SerializeException- If the format cannot represent this event in the current state.
-
number
Emits aTokenType.VALUE_NUMBERevent for aBigDecimal.- 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.SerializeException- If the format cannot represent this event in the current state.
-
number
Emits aTokenType.VALUE_NUMBERevent for aBigInteger.- 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.SerializeException- If the format cannot represent this event in the current state.
-
bool
Emits aTokenType.VALUE_BOOLEANevent.- Parameters:
value- The value.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.SerializeException- If the format cannot represent this event in the current state.
-
nil
Emits aTokenType.VALUE_NULLevent.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.SerializeException- If the format cannot represent this event in the current state.
-
object
Emits 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'sstartObject(),fieldName(String),string(String),number(long),bool(boolean),nil(), andbinary(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
fieldName(String)call, or inside an array. Callingobject(...)from inside an object body without a preceding field name throwsIllegalStateException, same as callingstring(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.- Parameters:
value- The value to emit. May benull .- Returns:
- This object.
- Throws:
IOException- If the underlying stream fails.
-
write
ImplementsRecordWriter.write(Object)by delegating toobject(Object).- Specified by:
writein interfaceRecordWriter- Parameters:
value- The value. May benull .- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.
-
binary
Emits aTokenType.VALUE_BINARYevent.Text formats normally encode the value as base64-encoded
TokenType.VALUE_STRING; the exact encoding is format-defined.- 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.SerializeException- If the format cannot represent this event in the current state.
-
isStreaming
Structural token cursors are always O(1)-memory streamers.- Specified by:
isStreamingin interfaceRecordWriter- Returns:
- Always
true .
-
flush
Flushes any buffered output to the underlying stream.- Specified by:
flushin interfaceFlushable- Specified by:
flushin interfaceRecordWriter- Throws:
IOException- If a problem occurred flushing the underlying stream.
-
close
Closes 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- Throws:
IOException
-
writeTag
Emits a semantic tag prefixing the next value emit.Tags compose:
writeTag(1).writeTag(2).number(5)writestag(1)(tag(2)(5)). The tag header is written immediately and does NOT consume a map-key/value or array-element slot; that bookkeeping belongs to the wrapped value emit that follows.- Parameters:
tagNumber- The unsigned tag number (carried as alongfor the full unsigned 64-bit range).- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.UnsupportedOperationException- If the format does not support semantic tags.
-
writeSimple
Emits an opaque simple value. The corresponding read-side token isTokenType.VALUE_NULLwithTokenReader.getSimpleValue().- Parameters:
value- The simple value. Format-specific reserved encodings (e.g. CBOR major-7 codepoints 20-23, 25-27, 31 collide with bool/null/undefined/float/break) must not be used.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.UnsupportedOperationException- If the format does not support opaque simple values.
-
writeExt
Emits a typed-binary value (signed type byte + binary payload). The corresponding read-side token isTokenType.VALUE_BINARYwithTokenReader.getExtType()andTokenReader.getBinary().- Parameters:
type- The signed type byte (range-128..127).payload- The payload bytes. Must not benull.- Returns:
- This object.
- Throws:
IOException- If a problem occurred writing to the underlying stream.UnsupportedOperationException- If the format does not support typed-binary values.
-