junfan / google-gson

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

Strange behavior on Map serialization #478

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'm trying to serialize a generic typed map using gson.
As result of "toJson(...)" I get following string:
"{"java.awt.Point[x\u003d10,y\u003d10]":{"x":101,"y":101}}"

it seems that for key it just call "toString()" function instead of 
serialization.

On desirialization I get following exception:

Exception in thread "main" com.google.gson.JsonSyntaxException: 
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 
column 39

due to the bad json format.

Is there any workaround?

Thanks In Advance.

gabber

Original issue reported on code.google.com by i7gab...@googlemail.com on 9 Oct 2012 at 3:57

Attachments:

GoogleCodeExporter commented 9 years ago
Problem solved, my fault, sorry.
There is an option called ComplexMapKeySerialization.
It can be enabled using GsonBuilder.

GsonBuilder builder = new GsonBuilder();
builder.enableComplexMapKeySerialization();
builder.create();

Original comment by i7gab...@googlemail.com on 16 Nov 2012 at 4:35

GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 4 Feb 2013 at 3:33