Skip to main content

YAML Methodology

The YAML data type produced depends on the Java object type being serialized.

  • Primitives and primitive objects are converted to YAML scalars.
  • Beans and Maps are converted to YAML block mappings.
  • Collections and arrays are converted to YAML block sequences.
  • Anything else is converted to YAML string scalars (with quoting if necessary).

Data type conversions:​

POJO typeYAML typeExampleSerialized form
StringScalarserialize("foobar");foobar
NumberScalarserialize(123);123
BooleanScalarserialize(true);true
NullScalarserialize(null);null
Beans with properties of any type on this listBlock mappingserialize(new MyBean());p1: val1\np2: true
Maps with values of any type on this listBlock mappingserialize(new TreeMap());key1: val1\nkey2: true
Collections and arrays of any type on this listBlock sequenceserialize(new Object[]{1,"foo",true});- 1\n- foo\n- true

Date/time types (Calendar, Date, Temporal subtypes, Duration) are serialized natively as ISO 8601 strings. Swaps can be used to convert non-serializable POJOs into serializable forms (such as byte[] arrays to Base-64 encoded strings) or to override the default format for types that already have built-in support.

Quoting Rules​

YAML strings are quoted when they contain special characters or could be misinterpreted:

  • Strings that are YAML reserved words (null, true, false, yes, no, etc.) are double-quoted.
  • Strings that look like numbers are double-quoted.
  • Strings containing special YAML characters (:, #, {, }, [LINKLABEL_0], etc.) are double-quoted.
  • Strings with leading or trailing whitespace are double-quoted.
  • Empty strings are double-quoted.
  • Strings containing control characters (\n, \t, \b, etc.) are double-quoted with proper escape sequences.

Nested Structures​

YAML uses indentation to represent nested structures. Each level of nesting increases the indent by 2 spaces:

person:
name: John Smith
addresses:
- street: 100 Main Street
city: Anywhereville
state: NY
- street: 200 Oak Avenue
city: Sometown
state: CA

Empty maps and collections are serialized using flow-style notation ({} and [] respectively).