如何设置Java-SnakeYaml Dumperoptions忽略引号和'|'?

问题描述

我正在Java中使用Snakeyaml来转储Yaml文件,该文件随后会被另一个应用程序解析。

输出Yaml必须看起来像这样:

aKey: 1234
anotherKey: a string
thirdKey:
  4321:
    - some script code passed as a string
    - a second line of code passed as a string

到目前为止,最接近的是通过设置自卸车-这样的选项:

DumperOptions options = new DumperOptions();
options.setIndent(2);
options.setPrettyFlow(true);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
[...]
Map<String,Object> data = new LinkedHashMap<>();
Map<Integer,Object> thirdKey= new LinkedHashMap<>();
data.put("aKey",1234);
data.put("anotherKey","aString");
StringBuilder extended = new StringBuilder();
extended.append("- some script code passed as a string\n- a second line of code passed as a string\n");
String result = extended.toString();
thirdKey.put(4321,result);
data.put("thirdKey",thirdKey);
yaml.dump(data,writer);

但是,输出是这样的:

aKey: 1234
anotherKey: a string
thirdKey:
  4321: |
    - some script code passed as a string
    - a second line of code passed as a string

光是|是使我无法实现最终目标的原因。

有什么方法可以设置dumperoptions来中断这样的行,而无需插入|。符号?

解决方法

问题是您生成列表标记-作为内容的一部分。他们不是!它们是YAML语法的一部分。

由于YAML中需要的是一个序列(列表的YAML术语),因此您必须在结构中放入一个列表:

thirdKey.put(4321,Arrays.asList(
  "some script code passed as a string","a second line of code passed as a string"));