YAML Parsers
The YamlParser class is used to parse YAML into POJOs.
The class hierarchy for the builder of this parser is:
Refer to the builder javadocs for configurable settings.
The following pre-configured parsers are provided for convenience:
The YAML parser supports:
- Block-style mappings and sequences (indentation-based).
- Flow-style mappings (
{key: value}) and sequences ([item1, item2]). - Plain, single-quoted, and double-quoted scalars.
- Literal block scalars (
|) and folded block scalars (>). - Comments (
# ...). - Null values (
null,~). - Booleans (
true,false). - Integer and floating-point numbers.
- Document markers (
---,...). - Bean type dictionaries via
_typeproperty.
Usage
// Parse YAML into a POJO
MyBean bean = YamlParser.DEFAULT.read(yaml, MyBean.class);
// Parse into generic types
JsonMap map = YamlParser.DEFAULT.read(yaml, JsonMap.class);
JsonList list = YamlParser.DEFAULT.read(yaml, JsonList.class);
// Parse with generic type parameters
List<MyBean> beans = YamlParser.DEFAULT.read(yaml, List.class, MyBean.class);
// Use the Yaml marshaller for convenience
MyBean bean = Yaml.to(yaml, MyBean.class);
Example
public class Person {
public int age;
public String name;
public List<String> tags;
}
String yaml = """
# A person
age: 5
name: Zed
tags: [a, b]
""";
Person p = YamlParser.DEFAULT.read(yaml, Person.class);
Block and flow styles can be mixed freely, so tags: [a, b] and a - block sequence produce the same result.
Parser settings
YamlParser has no format-specific builder settings. It inherits the standard settings from
ReaderParser / Parser / MarshallingContext (for example trimStrings(), bean dictionaries, swaps and
ignoreUnknownBeanProperties()). Media types consumed: application/yaml, text/yaml.
The @YamlConfig annotation (see YAML Serializers) only carries serializer-side
settings today.
Limitations compared to JSON
- Maps with non-
Stringkeys (Boolean,Date,Temporal,Enum) may not round-trip when the target is a genericMap;LinkedHashMap/TreeMapwithStringkeys work reliably. - There is no strict vs non-strict mode; YAML has no equivalent of JSON's lax parsing of unquoted attributes or concatenated strings.
- Indentation is significant; malformed indentation can cause parse errors where equivalent JSON would parse.