Class HjsonSerializer

All Implemented Interfaces:
Function<Object,String>, ThrowingFunction<Object,String>, HjsonMetaProvider, RecordWritable

Serializes POJO models to Hjson (Human JSON).
Media types

Handles Accept types: application/hjson, application/hjson+json

Produces Content-Type types: application/hjson

Description

Hjson is a syntax extension of JSON designed for human-friendly configuration files and hand-edited data. This serializer produces output that is valid Hjson and can be parsed back by HjsonParser.

The conversion is as follows:

  • Maps (e.g. HashMaps, TreeMaps) are converted to Hjson objects.
  • Collections (e.g. HashSets, LinkedLists) and Java arrays are converted to Hjson arrays.
  • Strings are serialized as quoteless values when they contain only safe characters. Strings that would be misinterpreted as booleans, null, or numbers are automatically double-quoted.
  • Numbers (e.g. Integer, Long, Double) are converted to Hjson numbers.
  • Booleans are converted to true or false.
  • null values are converted to Hjson null.
  • Beans are converted to Hjson objects.

The types above are considered "Hjson-primitive" object types. Any non-Hjson-primitive object types are transformed into Hjson-primitive object types through ObjectSwaps associated through the MarshallingContext.Builder.swaps(Class...) method. Several default transforms are provided for transforming Dates, Enums, Iterators, etc.

Example:

// Use one of the default serializers to serialize a POJO String hjson = HjsonSerializer.DEFAULT.write(someObject); // Create a compact serializer (comma-separated, no newlines between members) HjsonSerializer compact = HjsonSerializer.create().compact().build(); // Clone an existing serializer and enable multiline strings compact = HjsonSerializer.DEFAULT.copy().useMultilineStrings(true).build();

Example output - readable mode (Map of name/age):

{ name: Alice age: 30 }

Example output - compact mode:

{name:Alice,age:30}

Notes:
  • String values equal to true, false, null, or parseable as numbers are automatically serialized in double quotes to prevent parse ambiguity.
  • Empty strings are always serialized as "".
  • Root-level objects always include enclosing braces {} by default. Use HjsonSerializer.Builder.omitRootBraces(boolean) to suppress them.
  • In compact mode, strings containing newlines are serialized as double-quoted strings with JSON escape sequences rather than multiline ''' blocks.
  • Format-specific field-level annotations (@Hjson) are supported but intentionally minimal in the initial implementation.
  • This class is thread safe and reusable.
See Also: