How to create Single Line Yaml Array using Jackson in Java

Viewed 373

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?

0 Answers
Related