Gson错误“声明多个名为"的JSON字段.用于覆盖的继承字段 [英] Gson error "declares multiple JSON fields named" for overridden inherited fields

查看:668
本文介绍了Gson错误“声明多个名为"的JSON字段.用于覆盖的继承字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个课程:

父母

class Parent {
    @SerializedName("variable1")
    int variable1;

    @SerializedName("child")
    Child child;

    //Getters and Setters
}

孩子

class Child {
    @SerializedName("childVar")
    int childVar1;

    //Getters and Setters
}

这些扩展如下:

ExtendedChild

class ExtendedChild extends Child {
    @SerializedName("childVar2")
    int childVar2;

    //Getters and Setters
}

扩展父母

class ExtendedParent extends Parent {
    @SerializedName("variable2")
    int variable2; 

    @SerializedName("child")
    ExtendedChild extendedChild;

    //Getters and Setters
}

我想将这些类用于从Json取消编组.我打算使用GSON库.我想根据有效载荷取消编组到ExtendedParent或Parent类.我在代码中知道它是哪种类型的Parent.为了正确表示和避免多余的变量声明,我想使用继承.以下是我的两个示例-

I want to use these classes for un marshaling from Json. I am planning to use GSON library. I want to un marshal either to ExtendedParent or Parent classes based on the payload. I know in my code which type of Parent it is. Just to represent properly and to avoid redundant variable declaration, I wanted to go with Inheritance. Below are two examples I have with me-

我将把下面的有效载荷封送给ExtendedParent.class

I will marshal the below payload to ExtendedParent.class

{
    'variable1':12,
    'variable2':23,
    'child':{
        'childVar1': 43,
        'childVar2': 23
    }
}

我将把下面的有效载荷封送给Parent.class

I will marshal the below payload to Parent.class

{
    'variable1':12,
    'child':{
        'childVar1': 43
    }
}

但是,当我尝试以以下方式取消编组时,

However, when I try to to un marshal as below-,

ExtendedParent extendedParent = new Gson()
        .fromJson('<Passing the first Json string above>', ExtendedParent.class);

我面临以下例外.

java.lang.IllegalArgumentException:类ExtendedParent声明 多个名为child的JSON字段

java.lang.IllegalArgumentException: class ExtendedParent declares multiple JSON fields named child

无法找出问题所在.任何帮助或指针将不胜感激.我尝试了几种方法来避免在Child对象上隐藏变量,但无法解决该问题.

Not able to figure out what is the issue. Any help or pointers will be highly appreciated. I tried several ways to avoid variable hiding on the Child objects but I am not able to figure it out on how to solve this problem.

推荐答案

无论是Java字段名称还是序列化名称,GSON都不应对多次声明具有相同名称的字段.使用泛型来避免这种情况.像这样声明您的Parent:

GSON does not cope with fields declared multiple times having the same name whether it was the Java fieldname or serialize name. Use generics to avoid this kind of situation. Declare your Parent like:

public class Parent<T extends Child> {
    private T child;
    // any other stuff
}

ExtendedParent,例如:

public class ExtendedParent extends Parent<ExtendedChild> {
    // note no more declaring the field "child"
    // any other stuff
}

如果您需要使用名称extendedChild,则可以创建吸气剂&用child处理的该名称的设置器.

If you need to use name extendedChild you can just create getters & setters for that name that handle with child.

相关问题此处.

这篇关于Gson错误“声明多个名为"的JSON字段.用于覆盖的继承字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆