Class HoconSerializer

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

Serializes POJO models to HOCON (Human-Optimized Config Object Notation).
Media types

Accepts Accept types: application/hocon

Produces Content-Type types: application/hocon

Description

HOCON is a superset of JSON used extensively in the JVM ecosystem for configuration files. This serializer produces output that is valid HOCON and can be parsed back by HoconParser.

The conversion is as follows:

  • HashMap / bean → HOCON object with unquoted keys and = separator ({ key = value }).
  • Collection / array → HOCON array ([a, b, c]).
  • String (simple, no special chars) → unquoted string (name = myapp).
  • String (contains special chars or is ambiguous like "true", "42", "null") → quoted string (path = "/usr/local/bin").
  • String (multi-line) → triple-quoted string (desc = """...""").
  • Number → number literal (port = 8080).
  • Boolean → true or false.
  • null → null keyword.
  • Non-primitive types (e.g. Date, URI) → converted via ObjectSwap before serialization.
Behavior-specific subclasses

  • DEFAULT — readable, unquoted keys/values, = separator, root braces omitted (config-file style).
  • DEFAULT_BRACES — same as DEFAULT but includes root { } braces.
  • DEFAULT_COMPACT — single-line compact output, JSON-compatible.
Example:

// Serialize a bean String hocon = HoconSerializer.DEFAULT.write(myBean); // Create a custom serializer instance HoconSerializer s = HoconSerializer.create().omitRootBraces(false).build(); String hocon2 = s.write(myBean);

Example output (bean, root braces omitted):

name = myapp port = 8080 debug = true database { host = localhost port = 5432 } tags = [web, api, rest]

Example output (with root braces — DEFAULT_BRACES):

{ name = myapp port = 8080 debug = true }

Limitations:
  • Substitutions (${var}) are never emitted — the serializer always writes resolved (concrete) values.
  • Value concatenation is never emitted — the serializer always writes complete strings.
  • Path expressions (a.b.c = value) are never emitted — the serializer always uses nested-brace form.
  • include directives are never emitted — these require file-system access outside the scope of a serializer.
Notes:
  • This class is thread safe and reusable.
See Also: