Last active
March 31, 2024 15:14
-
-
Save marcel-dias/f1d0e25671d7f47b24271f15c1066ea3 to your computer and use it in GitHub Desktop.
SnakeYAML Parse Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
applications: | |
authentication: | |
serviceVersion: 2.0 | |
serviceUrl: https://myapp.corp/auth | |
appEnv: DEV | |
timeoutInMs: 5000 | |
enableLog: true | |
service1: | |
enableLog: true | |
authRequired: true | |
appEnv: DEV | |
timeoutInMs: 5000 | |
serviceUrl: https://myapp.corp/service1 | |
serviceName: SomeService1 | |
serviceVersion: 1.1 | |
serviceNamespace: http://myapp.corp/ns/service1 | |
service2: | |
enableLog: true | |
authRequired: true | |
appEnv: DEV | |
timeoutInMs: 5000 | |
serviceUrl: https://myapp.corp/service2 | |
serviceName: SomeService2 | |
serviceVersion: 2.0 | |
serviceNamespace: http://myapp.corp/ns/service2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.marceldias.mars.yaml; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.InputStream; | |
import org.yaml.snakeyaml.TypeDescription; | |
import org.yaml.snakeyaml.Yaml; | |
import org.yaml.snakeyaml.constructor.Constructor; | |
public class ParseSample { | |
public static void main(String... a) { | |
Constructor constructor = new Constructor(YamlConfig.class); | |
Yaml yaml = new Yaml( constructor ); | |
InputStream input = null; | |
try { | |
input = new FileInputStream(new File("/tmp/apps.yml")); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
YamlConfig data = yaml.loadAs( input, YamlConfig.class ); | |
System.out.println( data ); | |
System.out.println( yaml.dump( data )); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ServiceConfig { | |
private Boolean enableLog; | |
private Boolean authRequired; | |
private String appEnv; | |
private Integer timeoutInMs; | |
private String serviceUrl; | |
private String serviceName; | |
private String serviceVersion; | |
private String serviceNamespace; | |
public Boolean getEnableLog() { | |
return enableLog; | |
} | |
public void setEnableLog(Boolean enableLog) { | |
this.enableLog = enableLog; | |
} | |
public Boolean getAuthRequired() { | |
return authRequired; | |
} | |
public void setAuthRequired(Boolean authRequired) { | |
this.authRequired = authRequired; | |
} | |
public String getAppEnv() { | |
return appEnv; | |
} | |
public void setAppEnv(String appEnv) { | |
this.appEnv = appEnv; | |
} | |
public Integer getTimeoutInMs() { | |
return timeoutInMs; | |
} | |
public void setTimeoutInMs(Integer timeoutInMs) { | |
this.timeoutInMs = timeoutInMs; | |
} | |
public String getServiceUrl() { | |
return serviceUrl; | |
} | |
public void setServiceUrl(String serviceUrl) { | |
this.serviceUrl = serviceUrl; | |
} | |
public String getServiceName() { | |
return serviceName; | |
} | |
public void setServiceName(String serviceName) { | |
this.serviceName = serviceName; | |
} | |
public String getServiceVersion() { | |
return serviceVersion; | |
} | |
public void setServiceVersion(String serviceVersion) { | |
this.serviceVersion = serviceVersion; | |
} | |
public String getServiceNamespace() { | |
return serviceNamespace; | |
} | |
public void setServiceNamespace(String serviceNamespace) { | |
this.serviceNamespace = serviceNamespace; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Map; | |
public class YamlConfig { | |
Map<String, ServiceConfig> applications; | |
public Map<String, ServiceConfig> getApplications() { | |
return applications; | |
} | |
public void setApplications(Map<String, ServiceConfig> applications) { | |
this.applications = applications; | |
} | |
@Override public String toString() { | |
return "YamlConfig{" + | |
"applications=" + applications + | |
'}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment