2013-05-15 77 views
1

我一直在設置我的配置使用SnakeYAML。我想我的配置是這樣的:SnakeYAML不正確保存

connection: 
    bind-ip: 0.0.0.0 
    plugin-port: 2011 
    rtk-port: 2012 
    passphrase: abcde12345 
updates: 
    auto-update: true 
    channel: recommended 
    build: -1 

而是事實證明,作爲

connection: {bind-ip: 0.0.0.0, connection.passphrase: abcde12345, connection.pluginPort: 2011, 
    connection.rtkPort: 2012} 
updates: {updates.auto-update: true, updates.channel: Recommended, updates.build: -1} 

我有LinkedHashMap中,我保存到像這樣:

private LinkedHashMap<String, Map> values; 
public void set(String key, Map<String, Object> value) { 
     values.put(key, value); 
    } 

這裏是我使用填充方法/加載配置

private void init() { 
     Map<String, Object> connection = new LinkedHashMap<String, Object>(); 

     if (exists("connection.bind-ip")) { 
      bindIp = (String) get("connection.bind-ip"); 
     } else { 
      connection.put("bind-ip", bindIp); 
     } 

     if (exists("connection.passphrase")) { 
      passphrase = (String) get("connection.passphrase"); 
     } else { 
      connection.put("connection.passphrase", passphrase); 
     } 

     if (exists("connection.pluginPort")) { 
      pluginPort = (Integer) get("connection.rtkPort"); 
     } else { 
      connection.put("connection.pluginPort", pluginPort); 
     } 

     if (exists("connection.rtkPort")) { 
      rtkPort = (Integer) get("connection.rtkPort"); 
     } else { 
      connection.put("connection.rtkPort", rtkPort); 
     } 

     if (!(connection.isEmpty())) { 
      set("connection", connection); 
     } 

     Map<String, Object> updates = new LinkedHashMap<String, Object>(); 

     if (exists("updates.auto-update")) { 
      autoUpdate = (Boolean) get("updates.auto-update"); 
     } else { 
      updates.put("updates.auto-update", autoUpdate); 
     } 

     if (exists("updates.channel")) { 
      channel = (String) get("updates.channel"); 
     } else { 
      updates.put("updates.channel", channel); 
     } 

     if (exists("updates.build")) { 
      build = (Integer) get("updates.build"); 
     } else { 
      updates.put("updates.build", build); 
     } 

     if (!(updates.isEmpty())) { 
      set("updates", updates); 
     } 
    } 

任何幫助將不勝感激!對不起,它有點長

回答

0

簡單的答案。當您創建YAML時,請使用DumperOptions並設置DumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK)和您的設置。真不敢相信我錯過了:(

0

由於org.yaml:snakeyaml:1.17

DumperOptions options = new DumperOptions(); 
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK) 
new Yaml(options).dump(data, writer) 

只是想知道這是爲什麼默認情況下不