0

我最近尝试将对象转储到.yaml文件中,一切正常。但是,问题是我想要一个 ruby​​ 优化版本,因为输出文件由 ruby​​ 使用。目前,转储文件包含以下内容:

{foo: null, bar: null, foo1: null, bar1: null}

但是,我需要如下输出:

--- 
bar: ~
bar1: ~
foo: ~
foo1: ~

那么,我怎样才能使用snakeyaml 做到这一点。我在http://www.yamllint.com/获得了 ruby​​ 的 utf-8 优化版本。

4

1 回答 1

1

如果我理解您的问题,那么您可以使用yaml.dumpAsMap(map)代替yaml.dump(map), 和 aTreeMap然后String.replace(String, String)喜欢

Map<String, String> map = new TreeMap<>();
map.put("foo", null);
map.put("bar", null);
map.put("foo1", null);
map.put("bar1", null);

Yaml yaml = new Yaml();
String output = yaml.dumpAsMap(map); // yaml.dump(map);
System.out.println("---");
System.out.println(output.replace("null", "~"));

输出是(按要求)

------
bar: ~
bar1: ~
foo: ~
foo1: ~
于 2014-11-29T07:26:09.590 回答