mathwl / google-gson

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

Parsing a Map field with unquoted Integer keys fails in Gson 2.2.3 and above #524

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
(see Java code below)
1. Create a class with a field of type: Map<Integer, someClass>
2. Create a JSON string for that class where the key values of the map field 
are unquoted
3. Parse the above string with the Gson.fromJson method

What is the expected output? What do you see instead?
An exception is thrown:
java.lang.IllegalStateException: Expected an int but was STRING at line 1 
column 15
In the code below this exception is thrown only when parsing the string 
noQuotes, any only when using Gson versions 2.2.3 and 2.2.4.

What version of the product are you using? On what operating system?
The issue appears in Gson 2.2.3 and Gson 2.2.4, but not in Gson 2.2.2.
I don't know if this was a bug in 2.2.2 which was fixed, or a bug in the new 
versions. Anyway this is a backward-incompatibility and I didn't find any 
reference to it in the release notes of version 2.2.3.
OS: Win8 64bit

Please provide any additional information below.
Unquoted integers are parsed just fine when they are intended for primitive int 
fields, reference Integer fields, lists of Integers or Map values. As far as I 
can tell this issue appears only for Integers which are Map keys.

---- Java code ---

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.Map;

public class GsonTest {

    public static class TestClass {
        public Map<Integer, Integer> mapField = null;
    }

    public static void main(String[] args) throws Exception {
        Gson gson = new GsonBuilder().create();

        String quotes = "{\"mapField\": {\"5\": \"6\", \"7\": \"8\"}}";
        String keyQuotes = "{\"mapField\": {\"5\": 6, \"7\": 8}}";
        String noQuotes = "{\"mapField\": {5: 6, 7: 8}}";

        TestClass res = null;
        res = gson.fromJson(quotes, TestClass.class);
        res = gson.fromJson(keyQuotes, TestClass.class);
        res = gson.fromJson(noQuotes, TestClass.class);

    }
}

Original issue reported on code.google.com by yonmost on 18 Jun 2013 at 6:47

GoogleCodeExporter commented 9 years ago
there is a bug JsonReader.nextInt().  I've fixed it locally.  Will follow up to 
see how this can be pushed out.

Original comment by haripr...@gmail.com on 1 Aug 2013 at 3:13