skip to Main Content
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


  1. Chosen as BEST ANSWER

    Thank you @Thomas Boje / @Joachim Sauer,

        @Override
        public void run(String... args) throws Exception {
            final ObjectMapper jackson = new ObjectMapper();
            
            final ObjectNode objectNode = jackson.createObjectNode();
            String text = "Simplified Chinese 简体中文";
    
            // Enable escaping for non-ASCII characters, which is disabled by default
            jackson.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
            
            //no need to escape by ourselves, jackson will handle it after we enable the ESCAPE_NON_ASCII feature.
            //final String escapedInUnicodeText = StringEscapeUtils.escapeJava(text);
            //System.out.println(escapedInUnicodeText);
            //output is: Simplified Chinese u7B80u4F53u4E2Du6587
            
            objectNode.put("text", text);
            System.out.println(jackson.writeValueAsString(objectNode));
            //output is  {"text":"Simplified Chinese u7B80u4F53u4E2Du6587"}
        }
    

  2. 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:

    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.node.ObjectNode;
    import com.fasterxml.jackson.core.JsonGenerator;
    
    public class UnicodeExample {
        public static void main(String[] args) throws Exception {
            // Create ObjectMapper with custom settings
            ObjectMapper mapper = new ObjectMapper();
    
            // Disable the default escaping for non-ASCII characters
            mapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, false);
    
            // Example text with Unicode escapes
            String escapedInUnicodeText = "Simplified Chinese u7B80u4F53u4E2Du6587";
            
            // Create an ObjectNode and add the text
            ObjectNode objectNode = mapper.createObjectNode();
            objectNode.put("text", escapedInUnicodeText);
            
            // Serialize the object to JSON
            String jsonString = mapper.writeValueAsString(objectNode);
    
            // Output the result
            System.out.println(jsonString);
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search