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