Issue 501 in google-gson: Gson fails to parse simple map

464 views
Skip to first unread message

googl...@googlecode.com

unread,
Feb 15, 2013, 7:04:10 AM2/15/13
to google-gson...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 501 by phil...@meisen.net: Gson fails to parse simple map
http://code.google.com/p/google-gson/issues/detail?id=501

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.layout.list":"","server.rolemapper":"InsertionRoleMapper","xml.content.list":"data.xml"}}

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

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

googl...@googlecode.com

unread,
Mar 5, 2013, 11:18:12 AM3/5/13
to google-gson...@googlegroups.com

Comment #1 on issue 501 by bpgergo: Gson fails to parse simple map
http://code.google.com/p/google-gson/issues/detail?id=501

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.)

googl...@googlecode.com

unread,
Jul 10, 2013, 11:53:10 PM7/10/13
to google-gson...@googlegroups.com

Comment #2 on issue 501 by mark.but...@oi-sys.com: Gson fails to parse
simple map
http://code.google.com/p/google-gson/issues/detail?id=501

I have seen this problem also. It seems to occur when you have a map inside
a map. Again here is a sample JSON file generated by GSON, which validates
okay with JsonLINT, but GSON refuses to parse it. I have tried 2.2.2 and
2.2.4.



Attachments:
BBC.json 776 bytes

googl...@googlecode.com

unread,
Jul 11, 2013, 12:07:32 AM7/11/13
to google-gson...@googlegroups.com

Comment #3 on issue 501 by mark.but...@oi-sys.com: Gson fails to parse
simple map
http://code.google.com/p/google-gson/issues/detail?id=501

It's due to a problem in JsonReader ... for example consider this json

{
"mapA": {
"valA": 1,
"valB": 2
},
"mapB": {
"valC": 2,
"valD": 3
}
}

In MapTypeAdapterFactory.Adapter.read it calls JsonReader and hasNext is
still returning true after mapA finishes (I don't think it should), so it
carries on into mapB, but it returns null for both of the key values, so
when it gets to the second key it complains about "duplicate key: null"

googl...@googlecode.com

unread,
Jul 11, 2013, 12:22:56 AM7/11/13
to google-gson...@googlegroups.com

Comment #4 on issue 501 by mark.but...@oi-sys.com: Gson fails to parse
simple map
http://code.google.com/p/google-gson/issues/detail?id=501

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"

googl...@googlecode.com

unread,
Nov 22, 2013, 8:01:21 PM11/22/13
to google-gson...@googlegroups.com

Comment #5 on issue 501 by Initial....@gmail.com: Gson fails to parse
simple map
http://code.google.com/p/google-gson/issues/detail?id=501

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)

googl...@googlecode.com

unread,
Sep 8, 2014, 1:24:56 AM9/8/14
to google-gson...@googlegroups.com

Comment #6 on issue 501 by shiv...@totvslabs.com: Gson fails to parse
simple map
https://code.google.com/p/google-gson/issues/detail?id=501

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 ?

googl...@googlecode.com

unread,
Sep 11, 2014, 5:46:24 PM9/11/14
to google-gson...@googlegroups.com

Comment #7 on issue 501 by Maaarti...@gmail.com: Gson fails to parse simple
map
https://code.google.com/p/google-gson/issues/detail?id=501

@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.

googl...@googlecode.com

unread,
Sep 26, 2014, 12:29:01 PM9/26/14
to google-gson...@googlegroups.com

Comment #8 on issue 501 by shiv...@totvslabs.com: Gson fails to parse
simple map
https://code.google.com/p/google-gson/issues/detail?id=501

@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.

googl...@googlecode.com

unread,
Sep 29, 2014, 2:50:46 AM9/29/14
to google-gson...@googlegroups.com

Comment #9 on issue 501 by Maaarti...@gmail.com: Gson fails to parse simple
map
https://code.google.com/p/google-gson/issues/detail?id=501

@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.
Reply all
Reply to author
Forward
0 new messages