首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取不带class标签的Yaml字符串

获取不带class标签的Yaml字符串
EN

Stack Overflow用户
提问于 2016-07-01 18:10:22
回答 1查看 276关注 0票数 2

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

代码语言:javascript
复制
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]*");
}

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2019-08-14 20:47:02

将写入器配置更改为从不写类名,这样就可以了:

代码语言:javascript
复制
writer.getConfig().writeConfig.setWriteClassname(YamlConfig.WriteClassName.NEVER);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38142727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档