abhijeetjais20 / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

UTF-8 handling #392

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I searched web and API, could not figure out how to handle UTF-8. Any help is 
appreciated.

The request the service got is in UTF-8, I make sure it read correctly.

String strContent =CharStreams.toString(new InputStreamReader(is, "UTF-8"));

From the log, the strContent indeed has the following:

"Text": "\u20ACHello from UIE-3"

When I converted to json object, it appears have some issues:

Gson gson = new 
GsonBuilder().setPrettyPrinting().setFieldNamingPolicy(FieldNamingPolicy.UPPER_C
AMEL_CASE).create();

NotificationRequest nr = null;
try{
    nr = gson.fromJson(strContent, NotificationRequest.class);
    LOGGER.debug("NOTIFICATION REQUEST: {}", nr.toString());

The 'text' becomes:  Text=â¬Hello from UIE-3

What did I miss?

Thanks.

Jim 

Original issue reported on code.google.com by wguo1...@gmail.com on 20 Dec 2011 at 6:11

GoogleCodeExporter commented 9 years ago
I suspect that your IDE or terminal emulator is choking on the UTF-8 
characters. You can confirm this by printing the string.length() - if Gson is 
doing unicode properly then "\u20AC" will have length of 1. But a non-unicode 
capable terminal emulator will print two characters for the two bytes in the 
UTF-8 encoding.

If that is the problem, you should consider upgrading to a Unicode-capable 
terminal emulator. I use Terminator: http://software.jessies.org/terminator/

Original comment by limpbizkit on 21 Dec 2011 at 8:05

GoogleCodeExporter commented 9 years ago
Thanks a lot. You are absolutely right. I used putty as the terminal. When I 
open the log file on Windows, everything is displayed well. I blamed wrong lib 
:-(. 
Jim 

Original comment by wguo1...@gmail.com on 21 Dec 2011 at 8:19