Class JsonlSerializer
- All Implemented Interfaces:
Function<Object,,String> ThrowingFunction<Object,,String> JsonMetaProvider,ArrayRecordWritable,RecordWritable,TokenWritable
- Direct Known Subclasses:
Json5lSerializer
Media types
Handles
Produces
Description
Serializes POJOs to JSONL format where each top-level value is written as a compact JSON value on its own line. The conversion is as follows...
- Maps and beans are converted to JSON objects, one per line.
- Collections and arrays are converted to one JSON value per line (no wrapping brackets).
- Strings are converted to JSON strings, one per line.
- Numbers are converted to JSON numbers, one per line.
- Booleans are converted to JSON booleans, one per line.
- Nulls are converted to JSON nulls, one per line.
- Nested objects and collections remain compact on a single line per outer element.
Non-JSON-primitive types are transformed through
ObjectSwaps.
Example:
Example output (List of beans):
{"name":"Alice","age":30} {"name":"Bob","age":25} {"name":"Carol","age":35}
Complex (bean with nested object and array):
{"name":"Alice","age":30,"address":{"street":"123 Main St","city":"Boston"},"tags":["a","b","c"]} {"name":"Bob","age":25,"address":{"street":"456 Oak Ave","city":"Portland"},"tags":["d","e"]}
Limitations compared to JSON
- No pretty-printing:
useWhitespace()is always suppressed. JSONL requires each JSON value to fit on a single line; applying intra-line pretty-printing would embed newlines and produce invalid JSONL. The setting is silently ignored regardless of its configured value. - Top-level semantics differ: Collections and arrays are unwrapped — each element becomes one line. A single non-collection object produces one JSONL line.
Notes:
- This class is thread safe and reusable.
See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.juneau.marshall.json.JsonSerializer
JsonSerializer.Builder<SELF extends JsonSerializer.Builder<SELF>>, JsonSerializer.DefaultBuilder, JsonSerializer.ReadableNested classes/interfaces inherited from class org.apache.juneau.marshall.serializer.Serializer
Serializer.Null -
Field Summary
FieldsFields inherited from class org.apache.juneau.marshall.json.JsonSerializer
addBeanTypesJson, DEFAULT_READABLE, escapeSolidusFields inherited from class org.apache.juneau.marshall.serializer.WriterSerializer
maxIndent, quoteChar, quoteCharOverride, useWhitespaceFields inherited from class org.apache.juneau.marshall.serializer.Serializer
accept, addBeanTypes, addRootType, keepNullProperties, listener, nonDefault, produces, sortCollections, sortMaps, trimEmptyCollections, trimEmptyMaps, trimStringsFields inherited from class org.apache.juneau.marshall.MarshallingContextable
marshallingContextFields inherited from class org.apache.juneau.marshall.Context
CONTEXT_APPLY_FILTER -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic JsonSerializer.Builder<?>create()Creates a new builder for this object.Create a session builder based on the properties defined on this context.Methods inherited from class org.apache.juneau.marshall.json.JsonSerializer
copy, getJsonBeanPropertyMeta, getJsonClassMeta, getSchemaSerializer, getSession, isAddBeanTypes, isEscapeSolidus, properties, writeArrayRecords, writeTokensMethods inherited from class org.apache.juneau.marshall.serializer.WriterSerializer
applyThrows, getMaxIndent, getQuoteChar, getStreamCharset, isUseWhitespace, isWriterSerializer, println, quoteChar, toString, writeMethods inherited from class org.apache.juneau.marshall.serializer.Serializer
createSerializerBuilder, doWrite, forEachAcceptMediaType, getListener, getMediaTypeRanges, getPrimaryMediaType, getResponseContentType, getResponseHeaders, getUriContext, getUriRelativity, getUriResolution, isAddRootType, isKeepNullProperties, isNonDefault, isSortCollections, isSortMaps, isTrimEmptyCollections, isTrimEmptyMaps, isTrimStrings, write, writeToStringMethods inherited from class org.apache.juneau.marshall.MarshallingTraverseContext
getDetectRecursions, getIgnoreRecursions, getInitialDepth, getMaxDepth, isDetectRecursions, isIgnoreRecursions, shouldDetectRecursionsMethods inherited from class org.apache.juneau.marshall.MarshallingContextable
getDurationFormat, getMarshallingContext, getPeriodFormatMethods inherited from class org.apache.juneau.marshall.Context
createBuilder, getAnnotationProvider, init, isDebug, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.juneau.marshall.stream.ArrayRecordWritable
isArrayRecordStreaming, writeArrayRecordsMethods inherited from interface org.apache.juneau.marshall.stream.RecordWritable
isRecordStreamingMethods inherited from interface org.apache.juneau.commons.function.ThrowingFunction
applyMethods inherited from interface org.apache.juneau.marshall.stream.TokenWritable
writeRecords
-
Field Details
-
DEFAULT
Default serializer.
-
-
Constructor Details
-
JsonlSerializer
Constructor.- Parameters:
builder- The builder for this object.
-
-
Method Details
-
create
Creates a new builder for this object.- Returns:
- A new builder.
-
createSession
Description copied from class:ContextCreate a session builder based on the properties defined on this context.Use this method for creating sessions where you want to override basic settings. Otherwise, use
Context.getSession()directly.- Overrides:
createSessionin classJsonSerializer- Returns:
- A new session builder.
-