(gson 2.2.2) LazilyParsedNumber longValue not parsing consistently with Number documentation?

242 views
Skip to first unread message

Alex Piggott

unread,
Apr 30, 2013, 3:43:04 PM4/30/13
to googl...@googlegroups.com

I recently jumped from gson 1.6 to 2.2 and of course immediately everything stopped working :)

I discovered my MongoDB fields that I believed were long were actually double, sigh, ok fine - but gson 1.6 supported deserializing them to longs.

While investigating / working-around-using custom deserializers I noticed that JsonPrimitive.getAsNumber().longValue() was throwing an exception on things like "1.0" whereas the interface specification for Number clearly implied it would round/truncate: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Number.html#longValue()

It seems you have your own Number implementation, LazilyParsedNumber, which does not respect the (implied) behavior.

I've added custom deserializers, so it doesn't bother me anymore, but I thought I'd point it out in case it was worth fixing?

Alex

(ps Looking at http://grepcode.com/file/repo1.maven.org/maven2/com.google.code.gson/gson/2.2.2/com/google/gson/internal/LazilyParsedNumber.java?av=f, you could trivially support it by adding a 3rd try, which would call parseDouble and then cast to long)

Inderjeet Singh

unread,
Apr 30, 2013, 5:16:50 PM4/30/13
to googl...@googlegroups.com
Thanks for the bug report, Alex. Entered as http://code.google.com/p/google-gson/issues/detail?id=511
We will fix this in our next release.

Inderjeet Singh

unread,
May 13, 2013, 5:13:50 PM5/13/13
to googl...@googlegroups.com
Joel Leitch fixed this bug in https://code.google.com/p/google-gson/source/detail?r=1248 for release in Gson 2.2.4
Reply all
Reply to author
Forward
0 new messages