leogong / google-gson

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

Gson fails to parse simple map #501

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
When parsing the following json with gson (using 2.2.2 from maven-central), 
gson (fromJson) throws an:

JsonSyntaxException: "duplicate key: null"

whereby the json is created by gson (toJson) itself.

Json:
{"appProperties":{"server.configuration.reset":"false","server.db.jndi":"testDB"
,"xml.basedata.list":"basedata.xml","server.configuration.cache":"false","xml.la
yout.list":"","server.rolemapper":"InsertionRoleMapper","xml.content.list":"data
.xml"}}

http://json.parser.online.fr/ validates the string as correct...

Original issue reported on code.google.com by phil...@meisen.net on 15 Feb 2013 at 12:04

GoogleCodeExporter commented 9 years ago
I have the same issue, that is Gson.fromJson throws an JsonSyntaxException: 
"duplicate key: null"
whereby the json is created by Gson.toJson itself.

(I'm not posting the actual Json string because it may contain company 
information.)

Original comment by bpgergo on 5 Mar 2013 at 4:18

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I had the same problem but it turns out it was my fault - I had a map like this:

Map<Outcome, Integer> myMap

where Outcome is an enum. However in the enum, toString was not returning the 
name(). 

So GSON could write the JSON ok, but it could not deserialize it, because when 
it tried to create the enum, it didn't know how so it wrote a null, then when 
did this the second time it reported "duplicate key: null"

Original comment by mark.but...@oi-sys.com on 11 Jul 2013 at 4:22

GoogleCodeExporter commented 9 years ago
I have this issue too. My object extended a LinkedHashMap and I could serialize 
just fine (it would fill in the "class":"my.obj.MyClass", but when reading in 
json with a class keyword, it would fail with the "duplicate key: class" error.

Does anyone know if there is a way to ignore class keywords when creating the 
object, or fail and ignore the keyword, yet build the rest of the JSON object? 

My toString() method for the object MyClass is this: return new 
Gson().toJson(this);
I instantiate it from a file using this: return new Gson().fromJson(json, 
MyClass.class);
(json is one line from the data file I'm reading in in a String format)

Original comment by Initial....@gmail.com on 23 Nov 2013 at 1:01

GoogleCodeExporter commented 9 years ago
Hey guys, 

Anyone has any updates on this. We just hit this on production. We have a 
Map<Map<enum,Object>> and we got the same errors. Any workarounds ?

Original comment by shiv...@totvslabs.com on 8 Sep 2014 at 5:24

GoogleCodeExporter commented 9 years ago
@shiv: Map<Map<enum,Object>> makes no sense. The fact that you're getting the 
same message doesn't imply you ran into the same issue. Possibly, there's no 
issue, ask on SO instead.

AFAIK using a Map as a key is a problem. Using an enum defining toString is a 
problem, too, as `toString()` gets used for serialization, but the 
deserialization uses `name()`. This is wrong and should be (or has already 
been?) fixed.

Original comment by Maaarti...@gmail.com on 11 Sep 2014 at 9:46

GoogleCodeExporter commented 9 years ago
@Maaarti: I guess I didn't provide the full information. The real object that 
we convert back from json is: 
Map<String, List<Map<PassingParamEnum, Object>>>. And what we use to convert it 
is: 
Type mapType = new TypeToken<HashMap<String, List<Map<PassingParamEnum, 
Object>>>>()
        {
        }.getType();
        actualObject = gson.fromJson(theJsonString, mapType);

I am not using a map as a key. I am using a String as a key. I also have a LIST 
of a Map which is really where gson is giving us errors for Duplicate Keys. 

com.google.gson.JsonSyntaxException: duplicate key: PENDING_ACTION_ID

the PENDING_ACTION_ID is actually a key for this map: Map<PassingParamEnum, 
Object> and then a list of that Map is stored.

The weird thing is it doesn't happen all the times. Once a while it starts 
giving this error. And in the json string itself if you move around the key, it 
suddenly works. 

I am willing to provide the json string data where it SOMETIMES fails if anyone 
is interested in looking at it.

Original comment by shiv...@totvslabs.com on 26 Sep 2014 at 4:28

GoogleCodeExporter commented 9 years ago
@shiv: I'd strongly suggest to ask on stackoverflow.com where you may get an 
answer withing a few minutes. This issue list is not particularly responsive 
and it may or may not be an issue. This very issue mixes multiple problems over 
more than one year and makes little sense (if any). Without your data, I can't 
tell more, but with them, someone on SO will.

Original comment by Maaarti...@gmail.com on 29 Sep 2014 at 6:50