Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GSON - Not escaping unicode char

Tags:

java

json

gson

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?

like image 619
Jeffrey Avatar asked Jan 30 '26 20:01

Jeffrey


1 Answers

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.

like image 198
Denys Séguret Avatar answered Feb 01 '26 12:02

Denys Séguret



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!