Read and write YAML in Java using the Jakarta JSON API (JDK 11+)
Reader reader = ...;
try (JsonParser parser = Yaml.createParser(reader)) {
// Read YAML via the JsonParser
}
module-info.java:
module your.module {
// ...
requires io.xlate.yamljson;
// One of the following (if not using the --add-modules java option)
requires org.snakeyaml.engine.v2;
requires org.yaml.snakeyaml;
}
This library is built for multiple combinations of JSON API and Yaml versions.
Pick one of the two listed by choosing a dependency.
<dependency>
<groupId>io.xlate</groupId>
<artifactId>yaml-json</artifactId>
<version>${version.yaml-json.current}</version>
</dependency>
<dependency>
<groupId>io.xlate</groupId>
<artifactId>yaml-json</artifactId>
<version>${version.yaml-json.current}</version>
<classifier>legacy</classifier>
</dependency>
Placing one of the two supported YAML libraries on the class/module path will enable that library within yaml-json
.
Note, when using JPMS modules, you must also add the module via the java
command or your application's module-info.java
file.
If both libraries are present in your application, you can specify which to use by creating one of the factories in the
io.xlate.yamljson.Yaml
class and passing a configuration property with key io.xlate.yamljson.Yaml.Settings.YAML_VERSION
and one of the values io.xlate.yamljson.Yaml.Versions.V1_1
or io.xlate.yamljson.Yaml.Versions.V1_2
. Note, the values
specified here are the names of constants, NOT the values themselves.
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>${version.snakeyaml}</version>
</dependency>
Note: If using JPMS, also include org.yaml.snakeyaml
as required in module-info
or add to java
command: --add-modules=org.yaml.snakeyaml
<dependency>
<groupId>org.snakeyaml</groupId>
<artifactId>snakeyaml-engine</artifactId>
<version>${version.snakeyaml-engine}</version>
</dependency>
Note: If using JPMS, also include org.snakeyaml.engine.v2
as required in module-info
or add to java
command: --add-modules=org.snakeyaml.engine.v2