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
Mapsare converted to YAML block mappings. Collectionsand arrays are converted to YAML block sequences.- Anything else is converted to YAML string scalars (with quoting if necessary).
Data type conversions:
| POJO type | YAML type | Example | Serialized form |
|---|---|---|---|
| String | Scalar | serialize("foobar"); | foobar |
| Number | Scalar | serialize(123); | 123 |
| Boolean | Scalar | serialize(true); | true |
| Null | Scalar | serialize(null); | null |
| Beans with properties of any type on this list | Block mapping | serialize(new MyBean()); | p1: val1\np2: true |
Maps with values of any type on this list | Block mapping | serialize(new TreeMap()); | key1: val1\nkey2: true |
Collections and arrays of any type on this list | Block sequence | serialize(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).