fyhack / google-gson

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

Deserialization of long primitive types can lead to data corruption #82

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Deserializing a long > 32 bits from javascript into a java long primitive

What is the expected output? What do you see instead?
i don't think you guys handle long conversion from JSON -> Java properly.
In javascript, there is no such thing as a long (the language doesn't
support it - gwt plays tricks like using an array of doubles to keep
precision).

So, when passing back json, I have to quote my long id so it looks like a
string. However, Gson will try to cast the String to a Number which
obviously fails.
Could you change the default deserialization to take this case into
consideration?

Also, when serializing, I'm not so sure it makes sense to just convert a
long to a javascript number (like in your gson example doc).

Perhaps some sort of annotation on the that can be used to indicate it is
safe to cast a string to a long.

ie:

{ "myLongVar": "1000000" } should convert the String to a primitive long

but something like

{ "myLongVar" : 20000 } should also work without the quotes.

There are a lot of cases where you want a long as an id where you are not
performing math and should be safe to serialize/deserialize

What version of the product are you using? On what operating system?

Please provide any additional information below.

Original issue reported on code.google.com by dws...@gmail.com on 16 Dec 2008 at 5:40

GoogleCodeExporter commented 9 years ago
This is partially fixed and will be release with the next version of Gson.  r310
handles deserializing a string value into a long, int, byte, boolean, etc.

As for the JavaScript long support, there is no 32 bit restriction in the JSON
specification, but it probably makes sense to add a method to the GsonBuilder 
class
that will construct a Gson instance which serializes "long" fields as string 
(i.e.
quoted numbers) instead of a number field.

Original comment by joel.leitch@gmail.com on 17 Dec 2008 at 4:45

GoogleCodeExporter commented 9 years ago
Added ability for client to configure Gson to output all long (and Long) fields 
as
Strings.  Change made in r352.

Original comment by joel.leitch@gmail.com on 28 Dec 2008 at 3:41