【问题标题】:Getting Yaml string without class tags获取没有类标签的 Yaml 字符串
【发布时间】:2016-11-03 16:58:36
【问题描述】:

有没有一种方法可以使用 yamlbeans 库从 YamlWriter 的输出中隐藏所有标签?

public <T> String toYaml(T object) {
    try (StringWriter stringWriter = new StringWriter()) {
        YamlWriter writer = new YamlWriter(stringWriter);
        writer.write(object);
        writer.getConfig().writeConfig.setWriteRootTags(false);//replaces only root tag
        writer.close(); //don't add this to finally, because it the text will not be flushed
        return removeTags(stringWriter.toString());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

private String removeTags(String string) {
    //a tag is a sequence of characters starting with ! and ending with whitespace
    return removePattern(string, " ![^\\s]*");
}

谢谢。

【问题讨论】:

    标签: java serialization tags yaml yamlbeans


    【解决方案1】:

    将 writers 配置更改为从不写入类名,然后您就可以开始了:

    writer.getConfig().writeConfig.setWriteClassname(YamlConfig.WriteClassName.NEVER);
    

    【讨论】:

      猜你喜欢
      • 2019-08-12
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 2011-05-30
      • 2012-01-19
      • 2016-05-12
      相关资源
      最近更新 更多