Search code examples
javayamlsnakeyaml

Is it possible to obtain an yaml property at runtime?


yaml is very convenient because you are getting a nice deserialization from yaml to java but in my case I need to be able to obtain the property value at runtime. Is there a way to accomplish this as if you were using .properties file instead, for example, just by having the property key value?

appName: myAppName

Here I'd like to obtain myAppName value at runtime using the snakeyaml library.


Solution

  • Yes yaml.load() basically returns a java object so you can simply cast it to the appropriate type and get what you need:

        Yaml yaml = new Yaml();
        String input = "{appName: myAppName, appVersion: myAppVerison}";
        Map yamlMap = (Map)yaml.load(input);
        assertEquals("myAppName", yamlMap.get("appName"));