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="{"width":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?
Strings in JSON are specified using double quotes, i.e., " . If the strings are enclosed using single quotes, then the JSON is an invalid JSON .
Answer #1:JSON requires double quotes for its strings.
JavaScript object literals do not require quotes around a key name if the key is a valid identifier and not a reserved word. However, JSON always requires quotes around key names.
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);
JsonGenerator.Feature.QUOTE_FIELD_NAMES
is deprecated, you can use this instead:
mapper.configure(JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature(), false);
mapper.configure(JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES.mappedFeature(), true);
Note that JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES
is not deprecated (as of now), the corresponding JsonReadFeature
is mentioned here just for completeness.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With