I am parsing a YAML file
Props:
Prop1 : [10, 22, 20]
Prop2 : [20, 42, 60]
This gives me Map<String, Map<String, ArrayList<Integer>>>
I would like to get Map<String, Map<String, Integer[]>>
I do not want to convert List<Integer> to Integer[]
in the code that reads the file. Can I change something in my YAML file?
In contrast to my other answer, this one focuses on changing the YAML file. However, you also need to add some Java code to tell SnakeYaml how to load the tag you use.
You could add tags to the YAML sequences:
This need to be registered with SnakeYaml before loading:
You use it like this:
From the snakeyaml documentation:
There is no easy way to change it. Just call
toArray()
on a list and you're done.If the layout of your YAML file is stable, you can map it directly to a Java class, which defines the types of the inner properties:
Then, you can load it like this:
I used standard Java naming for the properties, which would require you to change the keys in your YAML file to lower case:
You can also keep the upper case, but you'd need to rename the Java properties accordingly.