I have a code that is dumping a linkedhashmap into a YAML object
public class TestDump {
    public static void main(String[] args) {
        LinkedHashMap<String, Object> values = new LinkedHashMap<String, Object>();
        values.put("one", 1);
        values.put("two", 2);
        values.put("three", 3);
        DumperOptions options = new DumperOptions();
        options.setIndent(2);
        options.setPrettyFlow(true);
        Yaml output = new Yaml(options);
        File targetYAMLFile = new File("C:\\temp\\sample.yaml");
        FileWriter writer =null;
        try {
            writer = new FileWriter(targetYAMLFile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        output.dump(values, writer);
    }
}
But the output looks like this
{
  one: 1,
  two: 2,
  three: 3
}
is there a way to set this to something like this
  one: 1
  two: 2
  three: 3
Although the first one is a valid yaml..I wanted the output format to be like the second one.
It looks like this is just some configuration via DumperOptions:
public class TestDump {
    public static void main(String[] args) {
        LinkedHashMap<String, Object> values = new LinkedHashMap<String, Object>();
        values.put("one", 1);
        values.put("two", 2);
        values.put("three", 3);
        DumperOptions options = new DumperOptions();
        options.setIndent(2);
        options.setPrettyFlow(true);
        // Fix below - additional configuration
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        Yaml output = new Yaml(options);
        File targetYAMLFile = new File("C:\\temp\\sample.yaml");
        FileWriter writer =null;
        try {
            writer = new FileWriter(targetYAMLFile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        output.dump(values, writer);
    }
}
This will solve my problem
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With