微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Jackson 将 YAML 反序列化为 Java 对象时在新行中保留缩进

如何解决使用 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.

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

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

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

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

有什么办法可以将其配置为在每一行中也包含缩进吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。