Gson异常——de-gson复杂对象

发布于 2024-11-07 13:59:32 字数 2409 浏览 0 评论 0 原文

我有一个包含

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小沫ゞ 2024-11-14 13:59:32

您遇到的 Gson 限制涉及它如何序列化映射键:通过调用 toString()。来自 MapTypeAdapter

map.add(String.valueOf(entry.getKey()), valueElement);

MapTypeAdapter 文档中也描述了此行为。

这个实现实际上只适用于简单的原始类型作为映射键。如果键不是简单的原语,则该对象将被 {@code toString}ed 并使用该值作为其键。

如果您坚持使用自定义类型作为映射键,那么我可以告诉您,您将必须编写一个自定义序列化器和/或自定义反序列化器和/或生成的 toString()易于反序列化的字符串表示形式。

另外,请查看 MapAsArrayTypeAdapter 是一种方法。 (它可以通过调用 GsonBuilder.enableComplexMapKeySerialization() 来使用,而不是通过文档描述的直接实例化(因为它当前不是公共类)。我没有测试它来查看它的实现是否有效,但看起来很有希望。)

注意: applicationIdToPortfoliosapplicationIdToAccounts 属性无需自定义处理即可进行序列化和反序列化,因为它们是带有原始类型键的映射。

The Gson limitation you're bumping up against concerns how it serializes map keys: by calling toString(). From MapTypeAdapter:

map.add(String.valueOf(entry.getKey()), valueElement);

This behavior is also described in the MapTypeAdapter documentation.

This implementation really only works well with simple primitive types as the map key. If the key is not a simple primitive then the object is {@code toString}ed and that value is used as its key.

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 and applicationIdToAccounts attributes serialize and deserialize simply without custom handling, since they are maps with primitive type keys.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文