Class JsonlTokenWriter
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable,RecordWriter,TokenWriter
TokenWriter surface for the JSONL / NDJSON
format.
Wraps a JsonTokenWriter and appends a newline after every top-level value emit. The
"top-level value" boundary is detected by tracking when an emit returns the cursor to
depth == 0.
Notes:
- This class is not thread safe.
-
Constructor Summary
ConstructorsConstructorDescriptionJsonlTokenWriter(Writer out, JsonTokenWriter.Settings settings) Constructor. -
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 JsonlTokenWriterforOutput(Object output, JsonTokenWriter.Settings settings) Internal factory that mirrorsJsonTokenWriter.forOutput(Object, JsonTokenWriter.Settings)so the per-formattokenWriter(...)factory can hand in any of the supported output types (Writer,OutputStream).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.Emits an arbitrary POJO through the format's structural events.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
write, writeExt, writeSimple, writeTag
-
Constructor Details
-
JsonlTokenWriter
Constructor.- Parameters:
out- The writer to emit JSONL to. Must not benull .settings- The output-formatting settings. Must not benull .
-
-
Method Details
-
forOutput
public static JsonlTokenWriter forOutput(Object output, JsonTokenWriter.Settings settings) throws IOException Internal factory that mirrorsJsonTokenWriter.forOutput(Object, JsonTokenWriter.Settings)so the per-formattokenWriter(...)factory can hand in any of the supported output types (Writer,OutputStream).- Parameters:
output- The output object.settings- The output-formatting settings.- Returns:
- A new
JsonlTokenWriterwriting to the resolved underlying writer. - Throws:
IOException- If the output type is not supported or could not be opened.
-
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.
-
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.
-
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.
-
isStreaming
Description copied from interface:TokenWriterStructural token cursors are always O(1)-memory streamers.- Specified by:
isStreamingin interfaceRecordWriter- Specified by:
isStreamingin interfaceTokenWriter- Returns:
- Always
true .
-
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
-