0

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 ?

user2315104
  • 2,378
  • 7
  • 35
  • 54
  • Isn't this the same question than you did yesterday https://stackoverflow.com/questions/76372591/how-to-add-indentation-in-yaml-using-snakeyaml-and-java-object? – aled Jun 01 '23 at 17:30
  • Yes , Im really blocked due to this . What can be done here ? Should I change my approach ? – user2315104 Jun 01 '23 at 18:18
  • I didn't found a way to customize the output format myself. Probably need to consider either accepting the output as is or finding a different library. – aled Jun 01 '23 at 18:29

1 Answers1

1

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