Jeffrey
Jeffrey

Reputation: 1118

GSON - Not escaping unicode char

I am using GSON to serialize my object. But some reason, it is not escaping certain unicode chars.

    Gson gson =  new Gson();
    System.out.println(gson.toJson("™"));
    System.out.println(Character.codePointAt("™", 0) );
    System.out.println(Character.codePointAt("™", 1) );

Output:

"™"

194

8482

Is there a setting to ensure that the chars get escaped?

Upvotes: 3

Views: 2609

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382130

There is no reason to escape unicode characters as they're allowed in JSON strings.

From json.org :

enter image description here

If you have problems sending your JSON to the browser, check your HTTP headers are set to UTF-8.

If you really want to escape unicode characters as in Java, you might use apache commons's StringEscapeUtils#escapeJava but you probably shouldn't.

Upvotes: 6

Related Questions