当我转储以下类的实例时:
class BrooklynApplicationEntity{
private String id;
private String location;
private String name;
List<BrooklynServiceEntity> services;
//getters and setters
...
}
使用下一个代码:
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setCanonical(false);
options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
SkipEmptyAndNullRepresenter skipEmptyAndNullRepresenter=
new SkipEmptyAndNullRepresenter();
Yaml yaml=new Yaml(skipEmptyAndNullRepresenter, options);
yaml.dump(this.getBrooklynApplicationEntity(), file);
我获得了下一个 yaml。
!!org.tomat.translate.brooklyn.entity.BrooklynApplicationEntity
id: dbApp
location: localhost
name: DatabaseApp
services:
- !!org.tomat.translate.brooklyn.entity.JBossBrooklynService
brooklynConfigProperties:
port.http: 80+
id: JBossMainWebServer
location: AWS
name: JBoss Main Web Server
- !!org.tomat.translate.brooklyn.entity.JBossBrooklynService
id: JBossSecondWebServer
location: localhost
name: JBoss
我想避免输出 YAML 中的 TAG,所以我添加了下一条指令,它在如何隐藏 bean 类型 in snakeyaml和ImplicitTagsTest中进行了描述。
skipEmptyAndNullRepresenter.addClassTag(JBossAgnosticElement.class, Tag.MAP);
skipEmptyAndNullRepresenter.addClassTag(JBossAgnosticElement.class, Tag.SEQ);
但是,TAG!!org.tomat.translate.brooklyn.entity.BrooklynApplicationEntity
和!!org.tomat.translate.brooklyn.entity.JBossBrooklynService
不会被删除。