How to hide bean types in sneakyaml
•
Java
This code will output: (yaml)
— !! org. test. bean. Person
Address: 4011 16th ave s
…..
You can hide my bean type (org. Test. Bean. Person) anyway! (prefer to use sneakyaml configuration... I can't find it...)
thank you!!
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);
}
Solution
Use org yaml. snakeyaml. representer. Delegate, set tag Map can hide the root label
Representer representer = new Representer();
representer.addClassTag(Person.class,Tag.MAP);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
