如何在snakeyaml中隐藏bean类型 [英] How to hide bean type in snakeyaml

查看:395
本文介绍了如何在snakeyaml中隐藏bean类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码将输出:(YAML)

This code will output:(YAML)

--- !! org.test.bean.Person

--- !!org.test.bean.Person

地址:4011 16th Ave S

address: 4011 16th Ave S

.....

可以隐藏我的bean类型( org.test.bean.Person)无论如何!
(更喜欢使用snakeyaml配置...我找不到它..)

Can hide my bean type(org.test.bean.Person) anyway !? (prefer to use snakeyaml config...i can't find it..)

谢谢!!

public static void dumpYAML(){
    Constructor constructor = new Constructor(Person.class);
    TypeDescription personDescription = new TypeDescription(Person.class);
    personDescription.putListPropertyType("phone", Tel.class);
    constructor.addTypeDescription(personDescription);

    Yaml yaml = new Yaml(constructor);
    Person person = (Person) yaml.load(makeYAML());

    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
    options.setCanonical(false); // display bean member attribute
    options.setExplicitStart(true); // display --- start

    yaml = new Yaml(options);
    String output = yaml.dump(person);
    System.out.println(output);
}


推荐答案

使用org.yaml.snakeyaml .representer.Representer,set Tag.MAP可以隐藏根标签。

Use org.yaml.snakeyaml.representer.Representer, set Tag.MAP can hide root tag.

    Representer representer = new Representer();
    representer.addClassTag(Person.class, Tag.MAP);

这篇关于如何在snakeyaml中隐藏bean类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆