当类具有抽象字段throw时,Gson反序列化json java.lang.RuntimeException:无法调用no-args构造函数

时间:2018-12-18 20:23:58

标签: java gson jsonserializer

df['AuxIndex'] = [i for i in range(len(df))]


def ScoreText(index):

    Sub1 = df['Sub1'][index]
    Sub2 = df['Sub2'][index]
    Sub3 = df['Sub3'][index]

    Score1 = df['S_score1'][index]
    Score2 = df['S_score2'][index]
    Score3 = df['S_score3'][index]

    if(Score1 + Score2 + Score3 == 0):

        return 'You have not scored'

    else:

        scoreText = 'You have scored on '+ '{x}{y}{z}'.format(x = ' {}({})'.format(Sub1,Score1) if Score1 > 0 else '',
                                                              y = ' {}({})'.format(Sub2,Score2) if Score2 > 0 else '',
                                                              z = ' {}({})'.format(Sub3,Score3) if Score3 > 0 else '' )

    return scoreText



df['S_Text'] = df['AuxIndex'].map(lambda i: ScoreText(i))

df.drop(columns = 'AuxIndex')

}

public class JsonSerializableTest {
protected static transient RuntimeTypeAdapterFactory<JsonSerializable> adapterFactory =
        RuntimeTypeAdapterFactory.of(JsonSerializable.class,"clazz")
        .registerSubtype(A.class,A.class.getName())
        .registerSubtype(B.class,B.class.getName())
        .registerSubtype(C.class,C.class.getName());
protected static transient Gson gson = new GsonBuilder().registerTypeAdapterFactory(adapterFactory).create();
protected static transient Type typeToken = new TypeToken<JsonSerializable>() {}.getType();
@Test
public void fromJson(){
    C c = new C();
    c = gson.fromJson(gson.toJson(c),typeToken);
    LogbackReporter.log(gson.toJson(c));

}

private abstract class A extends JsonSerializable{
    public A(){}
}

private class B extends A{
}

private class C extends JsonSerializable{
    public A a1 = new B();
    public A a2 = new B();
}

您可以从gson的git地址下载RuntimeTypeAdapterFactory。它不包含在番石榴中。

如果我删除抽象关键字,它可以,但是我想保留它。

有没有人可以帮助我?

0 个答案:

没有答案