Class JsonlSerializer

All Implemented Interfaces:
Function<Object,String>, ThrowingFunction<Object,String>, JsonMetaProvider, ArrayRecordWritable, RecordWritable, TokenWritable
Direct Known Subclasses:
Json5lSerializer

public class JsonlSerializer extends JsonSerializer
Serializes POJO models to JSONL (JSON Lines).
Media types

Handles Accept types: application/jsonl, application/jsonlines, application/x-ndjson, text/jsonl

Produces Content-Type types: application/jsonl

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:

// Use the default serializer to serialize a list of POJOs String jsonl = JsonlSerializer.DEFAULT.write(myList); // Create a custom serializer JsonlSerializer serializer = JsonlSerializer.create().build(); // Clone an existing serializer serializer = JsonlSerializer.DEFAULT.copy().build(); // Serialize a list of POJOs to JSONL String jsonl = serializer.write(myList);

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: