此代码将输出:(YAML)
--- !! org.test.bean.Person
地址:4011 16th Ave S
.....
无论如何都可以隐藏我的bean类型(org.test.bean.Person)! (更喜欢使用snakeyaml配置......我找不到它。)
谢谢!
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);
}
答案 0 :(得分:7)
使用org.yaml.snakeyaml.representer.Representer,设置Tag.MAP可以隐藏根标记。
Representer representer = new Representer();
representer.addClassTag(Person.class, Tag.MAP);
答案 1 :(得分:6)
您可以将Representsenter延伸至'偷偷摸摸地'将任何未注册的bean类注入Map。
public class MapRepresenter extends Representer {
@Override
protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
if (!classTags.containsKey(javaBean.getClass()))
addClassTag(javaBean.getClass(), Tag.MAP);
return super.representJavaBean(properties, javaBean);
}
}