javayamlsnakeyaml

Not able to format YAML using SnakeYaml keeping original way


Have following YAML

image:
  repository: "test.com/test"
  pullPolicy: IfNotPresent
  tag: "abc"

JAVA code to modify the YAKL file

public class SnakeYaml1 {

    public static void main(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub
        
        InputStream inputStream = new FileInputStream(new File("C:\\yaml\\student1.yaml"));
        
        Yaml yaml = new Yaml(new Constructor(Values1.class));
    
        Values1 data = yaml.load(inputStream);
        Image image = new Image();
        image.setPullPolicy("update");
        data.setImage(image);
        
        DumperOptions options = new DumperOptions();
        options.setIndent(2);
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
        options.setIndicatorIndent(2);
        options.setIndentWithIndicator(true);
        
        PrintWriter writer = new PrintWriter(new File("C:\\yaml\\student1.yaml"));
        Yaml yaml1 = new Yaml(new Constructor(Values1.class));
        yaml1.dump(data, writer);

    }
}


public class Values1 {
    
    private Image image;

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }
}

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;
    }
    

}

AFter executing the java code , the YAML format is getting changed

YAML format after executing JAVA code

!!oe.kubeapi.abc.Values1
image: {pullPolicy: update, repository: null, tag: null}

Expected YAML format after execution of java code

image:
      repository: "test.com/test"
      pullPolicy: update
      tag: "abc"

Not getting why the YAML format is getting changed after executing java code. Is this the bug in SnakeYaml ??

I tried putting property image in List format as well , List<Image> image still it did not work

please suggest . what should be done . Any help please ?


Solution

  • Well, you mentioned it is SnakeYaml lib, so I wonder have you ever looked through its documentation ?

    Your code works as it should.

    try:

     DumperOptions options = new DumperOptions();
     options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
     Yaml yaml  = new Yaml(options);