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

10,616

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.

Share:
10,616
Rey Libutan
Author by

Rey Libutan

Web enthusiast. Currently doing some frontend stuff :| . I miss backend.

Updated on June 23, 2022

Comments

  • Rey Libutan
    Rey Libutan almost 2 years

    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.

  • Rey Libutan
    Rey Libutan over 10 years
    This is the same solution I got. Glad you posted since I forgot to post my solution after I solved the problem :D
  • Pankaj Nimgade
    Pankaj Nimgade over 8 years
    how to serialize an object with UTF-8 encoding, help