I want to update (no override) my existing yaml file: YAML file looks like this:
firstName: "John"
#text here
lastName: "Doe"
age: 20
#This is important
city: "XYZ"
I have java class:
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String path = "E:/customer.yml";
Customer customer = objectMapper.readValue(new File(path), Customer.class);
customer.setAge(50);
objectMapper.writeValue(new FileWriter(path), customer);
And POJO model:
private String firstName;
private String lastName;
private int age;
//getters setters
How can I update yaml file without overriding it? Actually after run those class, value Age is modified, but other attributes like city is removed. I know, I can update my POJO model, and add city, but in my case, I'm not sure what is in file. I can convert file to String, and replace required data, but It'll not look professional.