fyhack / google-gson

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

BigDecimalTypeAdapter accepts invalid input when parsed alone #161

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
new Gson().fromJson("12345X67890", BigDecimal.class) yields a BigDecimal 
representing the number 12345. From the code it appears that 
BigDecimalTypeAdapter merely delegates parsing the String to BigDecimal 
itself, so the bug (or documented allowance) is there. However, this does 
not appear to be valid json, and in the context of json parsing an 
exception should be thrown. It would appear the type adapter needs to 
enforce a format on the input before constructing the BD.

Funny enough, it only passes in this situation. In these other three 
situations, an exception *does* occur:

1. Quoted String is given as input: new Gson().fromJson("\"12345X67890\"", 
BigDecimal.class)
2. Value is a field on an object, quoted : new 
Gson().fromJson("{\"price\":\"12345X67890\"}", StockQuote.class)
3. Value is a field on an object, unquoted: new 
Gson().fromJson("{\"price\":12345X67890}", StockQuote.class)

I don't get why this matters, since the same adapter, using the same code I 
can see is used each time... perhaps it's late and I'm missing something.

Gson version is 1.3.

Original issue reported on code.google.com by estebis...@gmail.com on 25 Sep 2009 at 3:22

GoogleCodeExporter commented 9 years ago
Err, sorry. Case #3 gives an error different from the first two. Doesn't even 
make it 
to the BigDecimalTypeAdapter, which is what I would expect in the original 
input that 
is allowed. Here are the exception messages by situation:

1. The JsonDeserializer BigDecimalTypeAdapter failed to deserialized json 
object 
"12345X67890" given the type class java.math.BigDecimal
2. The JsonDeserializer BigDecimalTypeAdapter failed to deserialized json 
object 
"12345X67890" given the type class java.math.BigDecimal
3. Failed parsing JSON source: java.io.StringReader@7f2a3793 to Json

Original comment by estebis...@gmail.com on 25 Sep 2009 at 3:26

GoogleCodeExporter commented 9 years ago
This is how the Gson's JSON parser works. In the first case, the lexical 
analyzer 
treats 12345 as one token and the remaining part as another. Your fromJson call 
returns fine because Gson doesn't look at the input beyond what it needs. Your 
other 
methods fail because they force Gson to look beyond the first token.

Original comment by inder123 on 25 Sep 2009 at 5:29

GoogleCodeExporter commented 9 years ago
So it's a documented feature of Gson's JSON parser that gargage is ignored in 
the 
stream? Seems like the overall JSON stream should fail, as this isn't valid 
JSON when 
viewed altogether.

Original comment by estebis...@gmail.com on 25 Sep 2009 at 6:31

GoogleCodeExporter commented 9 years ago
s/gargage/garbage/

bleh

Original comment by estebis...@gmail.com on 25 Sep 2009 at 6:32