2

有没有一种方法可以使用 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]*");
}

谢谢你。

4

1 回答 1

0

将作者配置更改为从不编写类名,您就可以开始了:

writer.getConfig().writeConfig.setWriteClassname(YamlConfig.WriteClassName.NEVER);
于 2019-08-14T12:47:01.830 回答