I have a yaml file, for example:
# this is the part I don't care about
config:
key-1: val-1
other-config:
lang: en
year: 1906
# below is the only part I care about
interesting-setup:
port: 1234
validation: false
parts:
- on-start: backup
on-stop: say-goodbye
Also I have a POJO class that is suitable for the interesting-setup part
public class InterestingSetup {
int port;
boolean validation;
List<Map<String, String>> parts;
}
I want to load just the interesting-setup part (similarly as @ConfigurationProperties("interesting-setup") in Spring)
Currently I'm doing it like this:
Map<String, Object> yamlConfig = yaml.load(yamlFile); # loading the whole file to Map with Object values
Object interestingObject = yamlConfig.get("interesting-setup"); # loading 'interesting-setup' part as an object
Map<String, Object> interestingMap = (Map<String, Object>); # Casting object to Map<String, Object>
String yamlDumped = yaml.dump(interestingMap); # Serialization to String
InterestingSetup finalObject = yaml.load(yamlDumped); # Getting final object from String
The crucial part is when I have an Object (Map<String, Object>) and want to cast it to my final class. To do that - I need to serialize it to String, so the process looks like this:
File -> Map<String, Object> -> Object -> Map<String, Object> -> String -> FinalClass
and I'd like to avoid deserialization and again serialization of the same data.
So can I somehow use Yaml to map the Map<String, Object> to another class? I cannot see this in an API?