I want to parse yaml files with dynamic properties. So, I have yaml files for different countries - netherlands.yml, usa.yml, uk.yml....etc
The content of the files will look like this-
netherlands:
name: netherlands
type: country
location:
latitude: aaa
longitude: bbb
amsterdam:
name: amsterdam
type: city
latitude: xxx
longitude: yyy
rotterdam:
name: rotterdam
type: city
latitude: ddd
longitude: ggg
hague:
name: hague
type: city
latitude: kkk
longitude: lll
I want to parse this and read it this way in my code -
@country(name="netherlands")
Country country;
country.getAmsterdam.getLatitude()
I am using Springboot and Java 11. How to achieve this using annotations? I believe I need to write a custom annotation for each country. But the main issue is the name of the cities will be dynamic for each country and number of cities will also vary from country to country. Also, cities can be added later in the yaml. I was able to write a code to parse the yaml and map it to my object. But what I saw till now is that, it needs to be mapped to a Class with fixed properties, which is not my case. This is the code I wrote but it does not serve my purpose.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
File file = new File(classLoader.getResource("netherlands.yml").getFile());
ObjectMapper om = new ObjectMapper(new YAMLFactory());
Country country = om.readValue(file, Country.class);
public class Country{
private String name ;
private String type;
private Location location;
// Here I need the dynamic attributes for my cities
}
I checked many articles but could not find any examples for something like this. Could you please suggest a way to achieve this? Thank you very much for your help.