Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SnakeYAML formatting - remove YAML curly brackets

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.

like image 713
Mark Estrada Avatar asked Dec 24 '22 07:12

Mark Estrada


1 Answers

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

like image 189
Mark Estrada Avatar answered Jan 10 '23 16:01

Mark Estrada