1

I would like to be able to preserve indentation when deserializing YAML to a Java object. If this is my YAML:

description: |
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
  Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 

   when an unknown printer took a galley of 
  type and scrambled it to make a type specimen book.

When I deserialize it, the description in my Java object looks like this:

description: "Lorem Ipsum is simply dummy text of the printing and typesetting industry.\n
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,\nwhen an unknown printer took a galley of\ntype and scrambled it to make a type specimen book."

As you can see it preserves the new lines, but not the trailing white-spaces in each line. This is how my Jackson mapper is configured:

YAMLFactory yamlFactory = new YAMLFactory()
            .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
            .enable(YAMLGenerator.Feature.MINIMIZE_QUOTES)
            .enable(YAMLGenerator.Feature.INDENT_ARRAYS);
    ObjectMapper objectMapper = new ObjectMapper(yamlFactory)
            .setSerializationInclusion(JsonInclude.Include.NON_NULL);

Is there any way that I can configure it to also include indendation in each line?

kimi1990mp
  • 101
  • 1
  • 13
  • [Related question](https://stackoverflow.com/q/60891174/347964). Jackson is too high level for this, you will get better results if you use SnakeYAML's event API (i.e. `Yaml.parse` for loading and the `Emitter` for writing back). – flyx Jan 20 '21 at 17:50

0 Answers0