google / gson

A Java serialization/deserialization library to convert Java Objects into JSON and back
Apache License 2.0
23.33k stars 4.28k forks source link

Null characters (\u0000) in strings? #906

Open DaveSanders opened 8 years ago

DaveSanders commented 8 years ago

This issue originally came up when I first reported it on the orhanobut/wasp library, and that issue contains specific examples and screenshots:

https://github.com/orhanobut/wasp/issues/145

While debugging Wasp to solve this, it turns out its gson.fromJson that is creating these strings. Basically when a bit of json like this:

{"service_code:"WDI"}

is deserialized into my object, the value that is put into the string is:

WDI\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000

Making comparisons such as this, not true:

object.service_code == "WDI" // => false

Is this a serialization issue with gson? Is it a memory thing of some sort? (I also saw an empty string get serialized with what looks to be other data.) Or is there some sort of builder setting I am not invoking to do this properly? The code that does the initialization of gson, and the fromJson is pretty drop dead simple:

new Gson() return gson.fromJson(content, type);

DaveSanders commented 8 years ago

A quick note, if I do object.service_code.equals("WDI"), then it does evaluate true. So my immediate issue is solved. But I'm still curious why these null characters happen.

swankjesse commented 8 years ago

Could you provide a test case?

tkrah commented 6 years ago

I'll do not have a SSCCE either but i am seeing the same nul bytes generated sometimes. Its even more worse - they are after "each" character. So having e.g. a property "login" in the object this becomes that in the string (it applies to anything in the string which is generated, properties, structure code, values etc.):

l\u0000o\u0000g\u0000i\u0000n\u0000

Any ideas where in the gson code this could happen so i can debug it there to get an idea about the cause and a possible fix @swankjesse ?

hmahajan commented 6 years ago

https://stackoverflow.com/questions/52894769/gson-json-output-contains-spaces-in-field-names-after-conversion

Dear Gson team, this happens intermittently. So far we are unable to reproduce it.