Search code examples
jsonjacksondouble-quotes

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


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?


Solution

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