YAML
Juneau supports converting arbitrary POJOs to and from YAML using native serializers and parsers implemented without any external dependencies. The YAML serializer converts POJOs directly to YAML block-style output using indentation-based structure. The YAML parser creates POJOs directly from YAML using an indentation-aware state machine.
The following example shows YAML for a typical bean:
Sample Beans
public class Person {
// Bean properties
public String name;
@MarshalledProp(calendarFormat=CalendarFormat.ISO_INSTANT) public Calendar birthDate;
public List addresses;
// Getters/setters omitted
}
public class Address {
// Bean properties
public String street, city;
public StateEnum state;
public int zip;
public boolean isCurrent;
// Getters/setters omitted
}
Sample Code
Person person = new Person()
.name("John Smith")
.birthDate("1946-08-12T00:00:00Z")
.addresses(
new Address()
.street("100 Main Street")
.city("Anywhereville")
.state(NY)
.zip(12345)
.isCurrent(true)
);
YAML Output
name: John Smith
birthDate: '1946-08-12T00:00:00Z'
addresses:
- street: 100 Main Street
city: Anywhereville
state: NY
zip: 12345
isCurrent: true
Serialization
// Use the default serializer to serialize a POJO to YAML
String yaml = YamlSerializer.DEFAULT.write(someObject);
// Use the marshaller for convenience
String yaml = Yaml.of(someObject);
Parsing
// Use the default parser to parse YAML into a POJO
MyBean bean = YamlParser.DEFAULT.read(yaml, MyBean.class);
// Use the marshaller for convenience
MyBean bean = Yaml.to(yaml, MyBean.class);
Media Types
- Produces:
application/yaml - Accepts:
application/yaml,text/yaml
Supported YAML Features
The parser supports:
- Block-style mappings and sequences
- 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
REST Integration
// Use BasicYamlConfig for YAML-only REST resources
@Rest
public class MyResource extends RestServlet implements BasicYamlConfig { ... }
// YAML is also included in BasicUniversalConfig
@Rest
public class MyResource extends RestServlet implements BasicUniversalConfig { ... }
See Also
- YAML Methodology for data type conversion details.
- YAML Serializers for serializer configuration.
- YAML Parsers for parser configuration.
- @YamlConfig Annotation for REST integration details.