【问题标题】:Java: How to avoid noise redundancy when use SnakeYaml?Java:使用 SnakeYaml 时如何避免噪声冗余?
【发布时间】:2015-02-10 12:57:39
【问题描述】:

我有通过 Java 创建的 yaml 文件。

--- !!com.test.users.Configuration
config:
  userList: !!set
    ? name: John
      age: 18
    : null
    ? name: Axel
      age: 14
    : null
  defaultUser:
    name: Bob
    age: 15

这是我的 SnakeYML 代码

DumperOptions options = new DumperOptions();
options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
options.setPrettyFlow(true);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setAllowReadOnlyProperties(true);
options.setExplicitStart(true);
yml = new Yaml(options);

FileWriter writer = new FileWriter(path);
yml.dump(obj, writer);

问题是如何排除所有这些多余的!!noise com.test.users.Configuration?: null!!set

【问题讨论】:

    标签: java snakeyaml


    【解决方案1】:

    1)

    Representer representer = new Representer();
    representer.addClassTag(com.test.users.Configuration.class, Tag.MAP);
    Yaml yaml = new Yaml(representer);
    

    2) 使用 List 而不是 Set 来避免 !!set:null

    【讨论】:

      猜你喜欢
      • 2012-11-17
      • 2011-10-20
      • 2019-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      • 2017-08-10
      相关资源
      最近更新 更多