asolfre / google-gson

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

GSon doesn't deserialize parametrized maps that are fields in a plain object #441

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
class Bar {
    String bar1;
    String bar2;
}

class Foo {
    String var1;
    Map<Bar,String> bars;
}

assuming that an instance of Foo was serialized, this will fail:

Foo foo = gson.fromJson(serializedString, Foo.class);

But since Foo is not generic, there does not seem to be a way to give gson a 
tokenizer that tells it what type to deserialize the bars field on Foo into. 
(And just out of curiosity, is there some magic which allows Morphia to handle 
maps as fields which Gson can't use?)

Thanks!

Original issue reported on code.google.com by christop...@gmail.com on 10 May 2012 at 12:13

GoogleCodeExporter commented 9 years ago
You need to register a TypeAdapter for Bar that writes it out as a single 
string. Gson has no idea how to take single strings and split them into Bar's 
two fields!

Original comment by limpbizkit on 30 Jun 2012 at 3:20