I have an object that contains
public class PositionsChannelApplicationGroups {
public PositionsChannelApplicationGroups(){}
private Map<MyObj1, List<Character>> portfoliosToApplicationIds = new HashMap<MyObj1, List<Character>>();
private Map<MyObj1, List<Character>> accountsToApplicationIds = new HashMap<MyObj2, List<Character>>();
private Map<Character, List<MyObj1>> applicationIdToPortfolios = new HashMap<Character, List<MyObj1>>();
private Map<Character, List<MyObj2>> applicationIdToAccounts = new HashMap<Character, List<MyObj2>>();
}
Now I try to gson it at the server and de-gson it at the client.
To make it simple I get an exception when I do this in one line
Gson gson = new Gson();
gson.fromJson(gson.toJson(object), PositionsChannelApplicationGroups.class);
or even
gson.fromJson(gson.toJson(object), new TypeToken<PositionsChannelApplicationGroups>(){}.getType());
but it gives me the following exception (below ).
what am I doing wrong ?
com.google.gson.JsonParseException: Expecting object found: "MyObj1{hibernateID=0, portfolioName='MyString'}"
at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:100)
at com.google.gson.ReflectingFieldNavigator.visitFieldsReflectively(ReflectingFieldNavigator.java:63)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:120)
at com.google.gson.JsonDeserializationContextDefault.fromJsonPrimitive(JsonDeserializationContextDefault.java:85)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:56)
at com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:67)
at com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:33)
at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:51)
at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:92)
at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:117)
at com.google.gson.ReflectingFieldNavigator.visitFieldsReflectively(ReflectingFieldNavigator.java:63)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:120)
The Gson limitation you’re bumping up against concerns how it serializes map keys: by calling
toString(). FromMapTypeAdapter:This behavior is also described in the
MapTypeAdapterdocumentation.If you insist on using custom types as map keys, then as best I can tell you’re going to have to write a custom serializer and/or a custom deserializer and/or a
toString()that generates a string representation that’s easy to deserialize.Also, take a look at MapAsArrayTypeAdapter for one approach. (It’s usable with a call to
GsonBuilder.enableComplexMapKeySerialization(), not through direct instantiation as the docs describe (because it’s currently not a public class). I didn’t test it to see if its implementation works, but it looks promising.)Note: The
applicationIdToPortfoliosandapplicationIdToAccountsattributes serialize and deserialize simply without custom handling, since they are maps with primitive type keys.