2

I want to create a yaml file as follows:

hello: World
value: 3
lists:['T1','T2']

I came up with below code:

public static void main(String[] args) throws IOException {

    Data data = new Data();
    data.setHello("World");
    data.setValue(3);
    data.setLists(Arrays.asList("T1","T2"));

    ObjectMapper objectMapper =
        new ObjectMapper(
            new YAMLFactory()
                .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
                .enable(YAMLGenerator.Feature.MINIMIZE_QUOTES)
                );
    
    objectMapper.writeValue(new File("filename.yaml"), data);
  }

  @Data
  private static class Data {
    private String hello;
    private int value;
    private List<String> lists;
  }

But this code returns the output as follows:

hello: World
value: 3
lists:
- T1
- T2

Can anyone guide me on how to get the single line array format of Yaml using jackson?

Ananya
  • 187
  • 3
  • 12
  • 1
    Jackson's API is too high level for that. You can do it with SnakeYAML but I advise not to force a specific style on your output because what you currently get is semantically equivalent and therefore will be processed the same way by every conforming YAML implementation. [You cannot exert complete control over the output format anyway](https://stackoverflow.com/q/60891174/347964). – flyx Nov 17 '21 at 14:19

0 Answers0