有没有一种方法可以使用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]*");
}谢谢。
发布于 2019-08-14 20:47:02
将写入器配置更改为从不写类名,这样就可以了:
writer.getConfig().writeConfig.setWriteClassname(YamlConfig.WriteClassName.NEVER);https://stackoverflow.com/questions/38142727
复制相似问题