繁体   English   中英

使用SnakeYAML编写YAML文件

[英]Writing a YAML file using SnakeYAML

我一直在尝试创建一种方法,该方法将我拥有的值组合到Yaml.load可以读取的Map中。 因此,例如:

Map<String, Object> map = new HashMap<String, Object>();
map.put("example.key.abc", "test");
map.put("example.key.def", "another test");
map.put("example.test", "yet another test");
map.put("example2.ex", "ex2");

我想将其转换为Yaml可以读取的地图。 我试过将abc和def与它们的值一起放入地图,然后将该地图与其他值一起放入地图。 我似乎永远无法使它正常工作。 另外,我希望它能够使用任何键,因此仅手动完成地图将无法工作。 如果我可以通过其余的代码运行Yaml可读的Map,则可以从上面的键获得以下输出:

example:
  key:
    abc: test
    def: another test
  test: yet another test
example2:
  ex: ex2

我只是似乎无法做出将我拥有的Map合并为Yaml可以理解的Map的方法。 是否有API,或者这只是使我无法弄清楚的一种非常简单的方法? 任何帮助表示赞赏,谢谢。

您对Guavas Splitter尤其MapSplitter #withKeyValueSeparator )的看法如何 ,并结合了mapTransformation之类的

Maps.transformEntries(fromMap, transformer)

所以生成的地图,您可以将它添加到snakeYaml

Map<String,Map<String,Map<String, String>>> =
  ImmutableMap.of("example", ImmutableMap.of("key", ImmutableMap.of("abc", "test")... 

顺便说一句:您从typesafe-config知道HOCON吗,该库支持读写点分隔(由Map组成)配置文件

foo.bar=10
foo.baz=12

Config foo = conf.getConfig("foo");
int bar2 = foo.getInt("bar");

要么

int bar1 = conf.getInt("foo.bar");

暂无
暂无

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

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