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

How to set Java - SnakeYaml Dumperoptions to leave out Quotations and '|'?

我在 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"));