简体   繁体   English

在标量值上使用字面样式与 Jackson 进行 YAML 序列化

[英]Use literal style on scalar values for YAML serialization with Jackson

I am using Jackson to serialize objects to YAML ( jackson-dataformat-yaml library).我正在使用Jackson将对象序列化为 YAML( jackson-dataformat-yaml库)。

I would like to produce literal style output for scalar values (eg 'description' in the following example) like我想为标量值(例如以下示例中的“描述”)生成文字样式输出,例如

---
id: 4711
description: |
  FooBar
  HelloWorld

but I only manage to produce quoted scalars like this:但我只能设法产生这样的引用标量:

---
id: 4711
description: "FooBar\nHelloWorld"

The code I use to generate the ObjectMapper is (by now) very simple:我用来生成 ObjectMapper 的代码(现在)非常简单:

    YAMLFactory f = new YAMLFactory();
    f.enable(YAMLGenerator.Feature.SPLIT_LINES); // setting does not matter
    ObjectMapper objectMapperYaml = new ObjectMapper(f);
    String yaml = objectMapperYaml.writeValueAsString(someObject);

I guess there is some possibility to generate literal style scalar values but I don't know how.我想有一些可能会生成文字样式标量值,但我不知道如何。 Any hints are welcome!欢迎任何提示!

If you would be using SNAKEYaml on it's own it would be a matter of setting the corresponding dumper option: 如果您自己使用SNAKEYaml,则需要设置相应的转储器选项:

DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultScalarStyle(ScalarStyle.LITERAL);

Sadly enough it can't be done through a JacksonFeature here. 可悲的是,这不能通过JacksonFeature来完成。

A quick glance through the source however show that the feature to enable is MINIMIZE_QUOTES , you'll find their algorithm in YAMLGenerator#writeString . 然而,快速浏览一下源显示要启用的功能是MINIMIZE_QUOTES ,您将在YAMLGenerator#writeString找到他们的算法。

So here's the full class: 所以这是全班:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;

public class NewClass {

    private int id;

    private String description;

    public static void main(String... a) throws JsonProcessingException {
        YAMLFactory f = new YAMLFactory();
        f.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES);
        ObjectMapper objectMapperYaml = new ObjectMapper(f);

        final NewClass someObject = new NewClass();
        someObject.setId(4711);
        someObject.setDescription("Hallo\nWorld!");
        System.out.println(objectMapperYaml.writeValueAsString(someObject));
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

I know it is an year old thread but this was the top link suggested by Google and it needs an update.我知道这是一年前的帖子,但这是 Google 建议的顶级链接,需要更新。

The literal style is supported by Jackson since v.2.9.自 v.2.9 以来,Jackson 支持文字样式。 There is an issue though with trailing spaces as in this bug尽管在此错误中尾随空格存在问题

Example:例子:

YAMLMapper mapper = new YAMLMapper();
mapper.configure(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE, true);
Map map = new HashMap();
map.put("literal_ok", "some value\n wih\n   multiple\n    new lines\nin it");
map.put("can_not_use_literal", "can not\n   use literal    \b because of the trailing spaces");
System.out.println(mapper.writeValueAsString(map));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM