How to let jackson generate json string using single quote or no quotes?

Freewind picture Freewind · Dec 23, 2012 · Viewed 18.9k times · Source

For example, I want to generate a json string for ng-style:

<th ng-style="{width:247}" data-field="code">Code</th>

But with jackson, the result is:

<th ng-style="{&quot;width&quot;:247}" data-field="code">Code</th>

It's not easy to read.

So I want jackson to generate the json string with single quote or no quotes. Is it possible to do this?

Answer

Perception picture Perception · Dec 27, 2012

If you have control over the ObjectMapper instance, then configure it to handle and generate JSON the way you want:

final ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);