DEV Community

Mihai A.
Mihai A.

Posted on

Modern YAML For Modern Java

Since Java does not have a standard API for YAML and since, starting YAML 1.2, the format is supposed to be a superset of JSON, we decided to create a parser which closely follows the API and encapsulation of JSON-P (JSR 374). With this idea in mind, eo-yaml took shape.

Unlike other YAML libraries on the market, eo-yaml is fully encapsulated and has an API-first design -- the user works with only a few intuitive Java Interfaces, all the implementations being hidden. This also opens the door for different providers: you don't like the reference implementation? Cool, implement those interfaces yourself and keep using the same API.

Usage

The entry point for building, reading, dumping or loading YAML is class com.amihaiemil.eoyaml.Yaml. Here's a short overview:

Building YAML

final YamlMapping team = Yaml.createYamlMappingBuilder()
    .add("architect", "amihaiemil")
    .add(
        "devops",
        Yaml.createYamlSequenceBuilder()
            .add("rultor")
            .add("0pdd")
            .build("DevOps Tools")
    ).add(
        "developers",
        Yaml.createYamlSequenceBuilder()
            .add("amihaiemil")
            .add("salikjan")
            .add("SherifWally")
            .build()
    ).build("Project Team");
System.out.println(team); //toString() methods overriden to pretty-print the YAML

The printed YAML will be:

# Project Team
architect: amihaiemil
# DevOps Tools
devops: 
  - rultor
  - 0pdd
developers: 
  - amihaiemil
  - salikjan
  - SherifWally

Reading YAML

Reading YAML is as simple as (you can read from a File, from an InputStream or from a String):

final YamlMapping team = Yaml.createYamlInput(
    new File("team.yml")
).readYamlMapping();

Dumping YAML (Bean-to-Yaml)

final YamlMapping student = Yaml.createYamlDump(
    new Student(...)//bean with getters and setters
).dump();

JDK Integration

The API is integrated with the JDK where possible: for instance, a YamlSequence implements Iterable<YamlNode> and a YamlStream (a collection of more YAML documents) implements Java 8's Stream API.

Java Module

The library is and will be primarily Java 8 compatible. However, it is packaged as a Module, so it can be used as such if you are on Java 9 or higher.

Easy Extension

Since it is based on interfaces, you can easily create your own decorators or implementations on top of the existing ones to enhance or create new functionality. More in the Wiki.

Often and Fast Releases

At the moment we release a new version containing fixes and features about once a week. We have a smart chatbot that lets us release to Maven Central with only one comment. See how the latest release went here.

To conclude, we hope we have provided a good overview of the product and hope to see as many users try it as possible. Feel free to open a Github Issue for any problem, question or feature request you may have, we'll try to help as fast as possible.

Top comments (0)