Gson异常——de-gson复杂对象
我有一个包含
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>>();
}
现在我尝试在服务器上对其进行 gson 并在客户端对其进行 de-gson。
为了简单起见,当我在一行中执行此操作时
Gson gson = new Gson();
gson.fromJson(gson.toJson(object), PositionsChannelApplicationGroups.class);
,甚至
gson.fromJson(gson.toJson(object), new TypeToken<PositionsChannelApplicationGroups>(){}.getType());
会出现异常,但它给出了以下异常(如下)。
我做错了什么?
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您遇到的 Gson 限制涉及它如何序列化映射键:通过调用
toString()
。来自MapTypeAdapter
:MapTypeAdapter
文档中也描述了此行为。如果您坚持使用自定义类型作为映射键,那么我可以告诉您,您将必须编写一个自定义序列化器和/或自定义反序列化器和/或生成的
toString()
易于反序列化的字符串表示形式。另外,请查看 MapAsArrayTypeAdapter 是一种方法。 (它可以通过调用
GsonBuilder.enableComplexMapKeySerialization()
来使用,而不是通过文档描述的直接实例化(因为它当前不是公共类)。我没有测试它来查看它的实现是否有效,但看起来很有希望。)注意:
applicationIdToPortfolios
和applicationIdToAccounts
属性无需自定义处理即可进行序列化和反序列化,因为它们是带有原始类型键的映射。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
MapTypeAdapter
documentation.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
applicationIdToPortfolios
andapplicationIdToAccounts
attributes serialize and deserialize simply without custom handling, since they are maps with primitive type keys.