Skip to content

devatherock/simple-yaml

Repository files navigation

CircleCI Download Lines of Code Coverage Status Quality Gate License: MIT

simple-yaml

A simple YAML generator for Java

Usage

For Gradle:

dependencies {
    implementation group: 'io.github.devatherock', name: 'simple-yaml', version: '0.3.0'
}

For Maven:

<dependencies>
    <dependency>
        <groupId>io.github.devatherock</groupId>
        <artifactId>simple-yaml</artifactId>
        <version>0.3.0</version>
    </dependency>
</dependencies>

Generating YAML

With default settings

Map<String, Object> map = new HashMap<>();
map.put("foo", "bar");
map.put("version", "1");
map.put("colors", Arrays.asList("red", "blue"));
String output = SimpleYamlOutput.toYaml(map);

Output:

foo: bar
version: 1
colors:
  - red
  - blue

With custom settings

Map<String, Object> map = new HashMap<>();
map.put("foo", "bar");
map.put("version", "1");
map.put("colors", Arrays.asList("red", "blue"));
SimpleYamlOutput yaml = SimpleYamlOutput.builder()
        .numericFieldToQuote("version")
        .flowStyleArrayField("colors")
        .indentArrays(false)
        .indentSize(3)
        .quoteType(SimpleYamlOutput.QuoteType.SINGLE)
        .build();
String output = yaml.dump(map);

Output:

foo: bar
version: '1'
colors: [ red, blue ]