Class TomlSerializer

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

Serializes POJOs to TOML v1.0.0 format.

TOML (Tom's Obvious Minimal Language) maps unambiguously to hash tables with native typing. See TOML v1.0.0 specification.

Media types

Produces: application/toml
Accepts: application/toml

Bean-to-TOML mapping
  • Simple properties → key-value pairs
  • Nested beans → [tableName] sections
  • Collections of beans → [[tableName]] array of tables
  • Collections of simple values → key = [a, b, c]
  • Maps with string keys → [mapName] table
  • Null properties are omitted (TOML has no null type)
Example:

// Serialize a bean to TOML String toml = TomlSerializer.DEFAULT.write(myBean); // Serialize a map (e.g. config with nested sections) Map<String, Object> config = Map.of("name", "myapp", "port", 8080); toml = TomlSerializer.DEFAULT.write(config); // Use the marshaller for convenience toml = Toml.of(config); // Custom serializer with whitespace for readability TomlSerializer s = TomlSerializer.create().ws().build(); toml = s.write(config);

Example output (Map of name/age):

name = "Alice" age = 30

Complex (nested table + array):

name = "Alice" age = 30 tags = ["a", "b", "c"] [address] street = "123 Main St" city = "Boston" state = "MA"

Limitations compared to JSON

TOML is a configuration format with a more restricted type system than JSON. The following features supported by JsonSerializer are not available or limited in TOML:

  • Null values — TOML has no null type. By default, null properties are omitted. Use keepNullProperties() to write the configured nullValue string (e.g. <NULL>) instead.
  • Map keys — TOML tables only support string keys. Maps with non-string keys (e.g. Map<Integer,String>) have keys converted via toString(); ensure results are valid TOML keys. Null keys are serialized as the string null.
  • Polymorphic types — addBeanTypes() and addRootType() add _type discriminators for polymorphic parsing; use with care as TOML structure differs from JSON.
  • Root-level collections — TOML expects a root table. Root arrays use array-of-tables ([[item]]) for beans/maps, or a _value wrapper for primitives and simple arrays.
  • Duplicate keys — TOML forbids duplicate keys; structures that would produce duplicate keys may yield invalid TOML.
Notes:
  • This class is thread safe and reusable.