2016-02-05 22 views
7

我是新來YAML和具有解析YAML配置文件看起來像:如何解析YAML文件的一部分SnakeYaml

applications: 
    authentication: 
    service-version: 2.0 
    service-url: https://myapp.corp/auth 
    app-env: DEV 
    timeout-in-ms: 5000 
    enable-log: true 

    service1: 
    enable-log: true 
    auth-required: true 
    app-env: DEV 
    timeout-in-ms: 5000 
    service-url: https://myapp.corp/service1 
    service-name: SomeService1 
    service-version: 1.1 
    service-namespace: http://myapp.corp/ns/service1 

    service2: 
    enable-log: true 
    auth-required: true 
    app-env: DEV 
    timeout-in-ms: 5000 
    service-url: https://myapp.corp/service2 
    service-name: SomeService2 
    service-version: 2.0 
    service-namespace: http://myapp.corp/ns/service2 

我要解析到以下Map結構

+==================================+ 
| Key    |    | 
+==================================+ 
| authentication | AuthConfig | 
+----------------------------------+ 
| service1   | ServiceConfig | 
+----------------------------------+ 
| service2   | ServiceConfig | 
+----------------------------------+ 

AuthConfigServiceConfig是我們系統中的自定義對象。

有人可以提供一些提示如何做到這一點?

+0

也許是不是一個有效的答案,但你可以使用YamlBeans代替:http://yamlbeans.sourceforge.net/,似乎更好的文件 –

+0

這個項目已被轉移到GitHub和那裏他們有很少的文件。事實上,這些文件比SnakeYaml差。我可能在這裏錯過了一些東西,但是你有鏈接到YamlBeans文檔嗎? – Niranjan

+0

是的,它已被移動:https://github.com/EsotericSoftware/yamlbeans在Github自述文件中有一個你想做什麼的解釋。 –

回答

3

Java有一個名爲Jackson的程序包,可處理YAML(以及JSON,CSV和XML)與Java對象之間的映射。您將遇到的大多數示例都是針對JSON的,但YAML鏈接顯示切換非常簡單。一切順利通過ObjectMapper

ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); 

,然後可以通過使用反射來反序列化對象:

ApplicationCatalog catalog = mapper.readValue(yamlSource, ApplicationCatalog.class); 

你會設置類是這樣的(我所做的一切公衆易於在JsonProperty annotation它重命名你的Java領域YAML場

class ApplicationCatalog { 
    public AuthConfig authentication; 
    public ServiceConfig service1; 
    public ServiceConfig service2; 
} 

class AuthConfig { 
    @JsonProperty("service-version") 
    public String serviceVersion; 
    @JsonProperty("service-url") 
    public String serviceUrl; 
    @JsonProperty("app-env") 
    public String appEnv; 
    @JsonProperty("timeout-in-ms") 
    public int timeoutInMs; 
    @JsonProperty("enable-log") 
    public boolean enableLog; 
} 

class ServiceConfig { 
    ... 
} 

注意:例如)。我覺得這是處理Java中的JSON和YAML最方便的方法。我還必須使用streaming API來拍攝真正大的物體。

+2

順便說一句,傑克遜實際上使用YAML引擎蓋下的SnakeYAML。 –

+0

感謝您的回覆;我知道傑克遜的API。我們正在將此用於我們的REST服務。我也知道jackson-yml包。但我不想在snakeyml庫上使用另一個包裝器。一旦我找到一個令人信服的解決方案,我會發布。無論如何,感謝您的幫助。 – Niranjan

-2

因此,當您在Auth和Service配置中具有相同的屬性時,我簡化了在一個類中的顯示。

的YamlConfig類是這樣的:

public class YamlConfig { 

    private Map<String, ServiceConfig> applications; 

    //... getter and setters here 
} 

而且解析器邏輯是類似的東西:

Yaml yaml = new Yaml(new Constructor(YamlConfig.class)); 
InputStream input = new FileInputStream(new File("/tmp/apps.yml")); 
YamlConfig data = yaml.loadAs(input, YamlConfig.class); 

我還對這個要點的完整代碼:https://gist.github.com/marceldiass/f1d0e25671d7f47b24271f15c1066ea3

+0

這是修改代碼的示例,而不是OP的原始請求。 OP的YAML文件使用破折號( - )作爲分隔符,而你的使用camelCase。 –