0

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

}
user2315104
  • 2,378
  • 7
  • 35
  • 54
  • Perhaps if you try `DumperOptions.FlowStyle.FLOW`, is that what you mean? – Alex R May 31 '23 at 10:43
  • No, still not . it is not working – user2315104 May 31 '23 at 11:32
  • What is Values? – aled May 31 '23 at 11:39
  • Please see the UPDATE 1 for values – user2315104 May 31 '23 at 11:41
  • There are other classes missing. Please provide a [minimal reproducible example](http://stackoverflow.com/help/minimal-reproducible-example). – aled May 31 '23 at 11:47
  • I have added all classes in UPDATE 2 . please suggest @aled – user2315104 May 31 '23 at 12:07
  • The input file does not match the definitions. Fields repository, pullPolicy don't exist in code. And what version of Snakeyaml are you using? – aled May 31 '23 at 13:33
  • repository and pullPolicy is there in Image class . Did you see that ? @aled SnakeYaml version : 1.33 – user2315104 May 31 '23 at 15:03
  • Yes, the error was confusing but the reason is that image is incorrectly typed as a `List` when clearly it is not a list in the input file. I'm not sure how that possibly worked. – aled May 31 '23 at 15:35
  • YAML generally doesn't give you complete control over formatting and doesn't let you keep the original format, see [this question](https://stackoverflow.com/q/60891174/347964) for details. It might be that you can achieve what you want in this scenario, but it's probably not worthwhile figuring that out since it won't be a general solution and it will probably break on different input / structure. – flyx Jun 01 '23 at 14:48

0 Answers0