How to parse part of a YAML file in SnakeYaml

I am new to YAML and parsing a YAML configuration file that looks like this:

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 

I need to analyze the following Map structure

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

AuthConfig and ServiceConfig are user objects in our system.

Can someone tell me how to do this?

+7
java yaml snakeyaml
source share
2 answers

There is a package for Java called Jackson that handles the mapping between YAML (and JSON, and CSV, and XML) and Java objects. Most of the examples you'll come across are for JSON, but the YAML link shows that the switch is direct. Everything goes through ObjectMapper :

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

This can then be used to deserialize the object through reflection:

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

You would create your classes something like this (I made everything public for the convenience of the example):

 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 { ... } 

Check out the JsonProperty annotation , which renames your Java field to a YAML field. I consider this the most convenient way to work with JSON and YAML in Java. I also had to use the streaming API for really large objects.

+3
source share

Since you have the same properties in the Auth and Service configuration, I simplified this in one class to show you here.

The YamlConfig class is as follows:

 public class YamlConfig { private Map<String, ServiceConfig> applications; //... getter and setters here } 

And the Parser logic looks something like this:

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

I have shared the full code in this context: https://gist.github.com/marceldiass/f1d0e25671d7f47b24271f15c1066ea3

-2
source share

All Articles