SnakeYAML
Required dependency
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.4</version>
</dependency>
implementation 'org.yaml:snakeyaml:2.4'
The newest version is always supported.
Adapter classes are:
Provider node
The JsonNode abstraction wraps a concrete class from JSON/YAML library, which is called a "provider node." It is possible to interact with the API using provider nodes directly, but there is no type information at compile time, so please ensure that you are using the correct class.
Constraints
Library is compatible with YAML 1.1 standard. However, there are few constraints:
all object keys are treated as strings,
object keys cannot be duplicated,
anchors and aliases are supported while override syntax (
<<
) is not.
Usage
Creating Validator instance
JsonNodeFactory factory = new SnakeYamlNode.Factory();
Validator validator = new ValidatorFactory()
.withJsonNodeFactory(factory)
.createValidator();
Using custom Yaml configuration
Yaml yaml = new Yaml();
JsonNodeFactory factory = new SnakeYamlNode.Factory(yaml);
Validator validator = new ValidatorFactory()
.withJsonNodeFactory(factory)
.createValidator();
Converting String to JsonNode
JsonNodeFactory factory = new SnakeYamlNode.Factory();
JsonNode jsonNode = factory.create("{}");
Converting provider node to JsonNode
org.yaml.snakeyaml.nodes.Node providerNode = new Yaml().compose(new StringReader("{}"));
JsonNodeFactory factory = new SnakeYamlNode.Factory();
JsonNode jsonNode = factory.wrap(providerNode);
Using Validator with provider nodes
org.yaml.snakeyaml.nodes.Node providerSchemaNode = new Yaml().compose(new StringReader("{}"));
URI schemaUri = validator.registerSchema(providerSchemaNode);
org.yaml.snakeyaml.nodes.Node providerInstanceNode = new Yaml().compose(new StringReader("true"));
Validator.Result result = validator.validate(schemaUri, providerInstanceNode);
04 August 2025