首页 > 解决方案 > 使用 Jackson 将 YAML 反序列化为 Java 对象时在新行中保留缩进

问题描述

我希望在将 YAML 反序列化为 Java 对象时能够保留缩进。如果这是我的 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.

当我反序列化它时,description在我的 Java 对象中看起来像这样:

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."

如您所见,它保留了新行,但不保留每行中的尾随空格。这就是我的杰克逊映射器的配置方式:

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

有什么方法可以将它配置为在每行中也包含缩进?

标签: javajacksonyamlsnakeyaml

解决方案


推荐阅读