【发布时间】:2014-12-18 09:05:55
【问题描述】:
当我转储以下类的实例时:
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,所以我添加了下一条指令, 在How to hide bean type in snakeyaml、ImplicitTagsTest 中是如何描述的。
skipEmptyAndNullRepresenter.addClassTag(JBossAgnosticElement.class, Tag.MAP);
skipEmptyAndNullRepresenter.addClassTag(JBossAgnosticElement.class, Tag.SEQ);
但是,标签!!org.tomat.translate.brooklyn.entity.BrooklynApplicationEntity 和!!org.tomat.translate.brooklyn.entity.JBossBrooklynService 不会被删除。
【问题讨论】: