2015-02-10 85 views
0

我有我通过Java创建的yaml文件。Java:使用SnakeYaml时如何避免噪声冗余?

--- !!com.test.users.Configuration 
config: 
    userList: !!set 
    ? name: John 
     age: 18 
    : null 
    ? name: Axel 
     age: 14 
    : null 
    defaultUser: 
    name: Bob 
    age: 15 

这里是我的SnakeYML代码

DumperOptions options = new DumperOptions(); 
options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN); 
options.setPrettyFlow(true); 
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); 
options.setAllowReadOnlyProperties(true); 
options.setExplicitStart(true); 
yml = new Yaml(options); 

FileWriter writer = new FileWriter(path); 
yml.dump(obj, writer); 

的问题是如何排除这些多余!!noise com.test.users.Configuration?: null!!set

回答

0

1)

Representer representer = new Representer(); 
representer.addClassTag(com.test.users.Configuration.class, Tag.MAP); 
Yaml yaml = new Yaml(representer); 

2)使用列表和未设置为避免!!set:null