How to stop Jackson's YAML script writer from quoting values

I am working on a project to convert files from JSON to YAML. I use version 2.8.3 of the following libraries:

  • JACKSON core
  • JACKSON-DataBind
  • JACKSON-DataFormat-YAML
  • JACKSON ANNOTATIONS

My YAML serialization code is very simple:

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
ObjectWriter writer = mapper.writer();

try {
    SequenceWriter sw = writer.writeValues(System.out);
    sw.write(tree);
}
catch (IOException e) {
    e.printStackTrace();
}

The YAML generated by this code is as follows:

serviceType: "elasticSearch"
displayName: "Elasticsearch Service"
description: "Sample Elastic Search Service"

Although this is true for YAML, I don't like double quotes around values. You do not need them in YAML, and this makes editing the resulting file more cumbersome. Does anyone know how to configure ObjectWriter to force jackson to stop encapsulating String values ​​in quotation marks?

+4
source share
1 answer

YAMLGenerator, MINIMIZE_QUOTES, .

enable() YAMLFactory :

ObjectMapper mapper = new ObjectMapper(new YAMLFactory().enable(Feature.MINIMIZE_QUOTES));
+4

Source: https://habr.com/ru/post/1656147/


All Articles