guochaiqi / google-gson

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

Maps do not serialize usefully #228

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I want to serialize a Map<MyObject, ...>. This is not currently possible 
because these are serialized into:

{"<myObject1.toString()>" : ..., "<myObject2.toString()>" : ..., ...}

which is obviously not reversible back into my MyObject instances.

I was able to fix this problem by writing a custom adapter that simply 
transforms the Map into a List on serialization and back into a Map on 
deserialization, but this is a pain when otherwise I can serialize a huge 
object hierarchy very simply.

e.g., I made the above serialize to [{"k" : myObject1, "v" : ...}, {"k" : 
myObject2, "v" : ...}, ...]

I propose that DefaultMapTypeAdapter use something like this List approach by 
default, unless the key value happens to be a String, since there is really 
nothing else particularly sane it can do without losing a ton of information in 
doing the String.valueOf() call.

Original issue reported on code.google.com by jsha...@google.com on 26 Aug 2010 at 9:02

GoogleCodeExporter commented 9 years ago
This is a dupe of issue 210.

Original comment by jsha...@google.com on 28 Aug 2010 at 6:23

GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 3 Sep 2010 at 6:50