public void run(String... args) throws Exception {
final ObjectMapper jackson = new ObjectMapper();
final ObjectNode objectNode = jackson.createObjectNode();
String text = "Simplified Chinese 简体中文";
final String escapedInUnicodeText = StringEscapeUtils.escapeJava(text);
System.out.println(escapedInUnicodeText);
//output is: Simplified Chinese u7B80u4F53u4E2Du6587
objectNode.put("text", escapedInUnicodeText);
System.out.println(jackson.writeValueAsString(objectNode));
//output is {"text":"Simplified Chinese \u7B80\u4F53\u4E2D\u6587"}
}
Result of System.out.println(escapedInUnicodeText);
is Simplified Chinese u7B80u4F53u4E2Du6587
Result of System.out.println(jackson.writeValueAsString(objectNode));
is {"text":"Simplified Chinese \u7B80\u4F53\u4E2D\u6587"}
.
Regarding the 2nd result, the generated JSON, how can I make \u
be u
?
2
Answers
Thank you @Thomas Boje / @Joachim Sauer,
You can use the JsonGenerator.Feature.ESCAPE_NON_ASCII feature, but also ensure that your string is not double-escaped during the serialization process.
To achieve this, you can configure Jackson to not escape Unicode characters when serializing. This involves using the Jackson ObjectMapper and customizing its serialization settings.
Here’s how I would do this: