Have following YAML
id: "20"
name: "Bruce"
year: "2020"
address: "Gotham City"
department: "Computer Science"
image:
repository: "test.com/test"
pullPolicy: "IfNotPresent"
tag: "dfdklf"
route:
hosts:
- host: abc.com
- host: abc1.com
- host: abc2.com
but when I'm adding new Host entry using JAVA object (serialization and deserialization) and SnakeYaml lib the indentation is not proper for Image . it is coming as like below
address: Gotham City
department: Computer Science
id: '20'
image: {pullPolicy: IfNotPresent, repository: test.com/test,
tag: dfdklf}
name: Bruce
route:
hosts:
- {host: abc.com}
- {host: abc1.com}
- {host: abc2.com}
year: '2020'
how to maintain the same structure as the original YAML ? I see that , image entry is modified and coming as a single line and the sequence is also not maintained.
Here is the JAVA code
public static void main(String[] args) throws FileNotFoundException {
InputStream inputStream = new FileInputStream(new File("C:\\yaml\\student.yaml"));
Yaml yaml = new Yaml(new Constructor(Values.class));
Values data = yaml.load(inputStream);
data.getImage().forEach(x -> System.out.println(x.getTag()));
data.getRoute().getHosts().forEach(x -> System.out.println(x.getHost()));
DumperOptions options = new DumperOptions();
options.setIndent(2);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setIndicatorIndent(2);
options.setIndentWithIndicator(true);
Host host = new Host();
host.setHost("abc.com");
data.getRoute().getHosts().add(host);
data.getRoute().getHosts().forEach(x -> System.out.println(x.getHost()));
PrintWriter writer = new PrintWriter(new File("C:\\yaml\\student.yaml"));
Yaml yaml1 = new Yaml(new Constructor(Values.class));
yaml1.dump(data, writer);
}
}
UPDATE 1 :
public class Values {
private List<Image> image;
private String id;
private String name;
private String year;
private String address;
private Route route;
public Route getRoute() {
return route;
}
public void setRoute(Route route) {
this.route = route;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
private String department;
public Values()
{
}
public Values (List<Image> image, String id, String name, String year, String address, String department )
{
super ();
this.image = image;
this.id = id;
this.name = name;
this.year = year;
this.address = address;
this.department = department;
}
public List<Image> getImage() {
return image;
}
public void setImage(List<Image> image) {
this.image = image;
}
/*
* public Map<String, Object> get(String string) { // TODO Auto-generated method
* stub return null; }
*/
}
UPDATE 2
public class Host {
private String host;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
}
public class Image {
private String repository;
private String pullPolicy;
private String tag;
public Image()
{
}
public Image (String repository, String pullPolicy, String tags)
{
super();
this.repository = repository;
this.pullPolicy = pullPolicy;
this.tag = tags;
}
public String getRepository() {
return repository;
}
public void setRepository(String repository) {
this.repository = repository;
}
public String getPullPolicy() {
return pullPolicy;
}
public void setPullPolicy(String pullPolicy) {
this.pullPolicy = pullPolicy;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
}
public class Route {
private List<Host> hosts = new ArrayList<Host>();
public List<Host> getHosts() {
return hosts;
}
public void setHosts(List<Host> hosts) {
this.hosts = hosts;
}
}