I'm trying to dump a YAML object to String but I'm facing some problems. This is the original YAML info:
trace_enabled: false
error_gen_enabled: false
trace_info:
filter: "filter-12345"
status: 200
method: "PUT"
error_gen_info:
code: 500
rate: 0.35
content_type: 'application/problem+json'
content: '{"status":503,"title":"Internal Server Error","detail":"Too busy","cause":""}'
If I try to dump with fasterxml.jackson I try the following:
ObjectMapper mapper = new ObjectMapper(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER));
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
try {
result = mapper.writeValueAsString(yamlObject);
} catch (JsonProcessingException e) {
log.warn("Error parsing YAML object to string. Message: {}. Cause: {}", e.getMessage(), e.getCause());
}
And results:
trace_enabled: false
error_gen_enabled: false
trace_info:
filter: "filter-12345"
status: 200
method: "PUT"
error_gen_info:
code: 500
rate: 0.35
content_type: "application/problem+json"
content: "{\"status\":503,\"title\":\"Internal Server Error\",\"detail\":\"Too busy\"\
,\"cause\":\"\"}"
So it can be seen in the original that "trace_info" String values are double quoted, and "error_gen_info" are single quoted. I'd like that "content_type" and "content" would be single quoted, as now is double quoted and the value json looks terrible.
Now if I try with snakeyaml I do:
Representer representer = new Representer();
representer.addClassTag(ErrorGenInfo.class, Tag.OMAP);
representer.setDefaultFlowStyle(FlowStyle.BLOCK);
TypeDescription errorGenInfoDesc = new TypeDescription(ErrorGenInfo.class);
errorGenInfoDesc.substituteProperty("content_type", String.class, "getContentType", "setContentType");
representer.addTypeDescription(errorGenInfoDesc);
errorGenInfoDesc.setExcludes("contentType");
Yaml yaml = new Yaml(representer);
result = yaml.dump(yamlObject);
And I get:
!!com.test.BasicTest$Config
errorGenEnabled: false
errorGenInfo:
code: 500
content: '{"status":503,"title":"Internal Server Error","detail":"Too busy","cause":""}'
contentType: application/problem+json
rate: 0.35
traceEnabled: false
traceInfo:
filter: filter-12345
method: PUT
status: 200
Here I get a different result:
- Why do I get the first line with the class? Is there any way to avoid it?
- It does not keep the order, but this is not so important.
- The "content" is processed as I want, but none of the String values are quoted. Is there any way to say that string values from "traceInfo" have to be double quoted and string values from "errorGenInfo" have to be single quoted?
- Important: the key names are are not separated by underscore, despite I added some code that I saw in other posts (testing TypeDescription.substituteProperty(...) with "content-type"). It seems is not working.
Having done this, how could I get the same format in the output with any of these 2 ways of processing YAML? Thanks in advance.
From the source code of Jackson's
YAMLGenerator, it didn't look like it was possible to single quote strings at all. The closest I could get to your expected output is usingSnakeYAMLdirectly. No String values were quoted except forcontent_typefield which was single quoted as expected. And the order of fields with the POJOs wasn't maintained. I did manage to remove the tag with the class name and also to rename the field names to be snake case.Code:
Output YAML: