Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize japanese chars in UTF-8 using GSON?

I am currently using a code (best answer on a question) I found here everything works properly until you give it a japanese String input.

I thought the UTF-8 charset would do the trick but I am not really sure what part of the code does not allow japanese characters to be serialized.

For example if I serialize something basic like "ひらがな" it will output garbage characters.

What I am doing is something like

String serialized = serialize("ひらがな");
String deserialized = deserialize(serialized, new TypeToken<String>() {}.getType());
System.out.println(deserialized);

But I am getting a garbage deserialized.

Can someone please shed some light? Thank you.

like image 931
Rey Libutan Avatar asked Oct 09 '13 16:10

Rey Libutan


1 Answers

I don't know the exact answer to your question, but I can say I had a similar problem and here was my solution. Maybe it's a hint for you:

I am only using GSON for deserialization. I had to change the following code from

json = gson.fromJson(new InputStreamReader(is), parseType);

to

json = gson.fromJson(new InputStreamReader(is,"UTF-8"), parseType);

So the issue was in my input stream reader, not GSON per se. I wonder if you need to use a string reader for deserialization or something. Sorry I can't give you a more specific answer.

like image 177
Bjorn Roche Avatar answered Oct 01 '22 20:10

Bjorn Roche