繁体   English   中英

SnakeYAML格式-删除YAML大括号

[英]SnakeYAML formatting - remove YAML curly brackets

我有一个代码,将链接的哈希映射转储到YAML对象中

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);
    }
}

但是输出看起来像这样

{
  one: 1,
  two: 2,
  three: 3
}

有没有办法将其设置为这样的事情

  one: 1
  two: 2
  three: 3

尽管第一个是有效的Yaml ..我希望输出格式像第二个一样。

看起来这只是通过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);
    }
}

这样可以解决我的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM