Serialization Gson RuntimeTypeAdapterFactory-要序列化的null项导致null指针异常

Serialization Gson RuntimeTypeAdapterFactory-要序列化的null项导致null指针异常,serialization,gson,Serialization,Gson,我正在测试RuntimeTypeAdapterFactoryTest: 在最初的示例测试用例(testRuntimeTypeAdapter())中,它工作得很好: 但如果注册的类型为null,则在RuntimeTypeAdapterFactory中会出现NPE异常。扩展上面的原始示例: static class Wallet { BillingInstrument payment; } Wallet wallet = new Wallet(); // wallet.payment

我正在测试RuntimeTypeAdapterFactoryTest:

在最初的示例测试用例(testRuntimeTypeAdapter())中,它工作得很好:

但如果注册的类型为null,则在RuntimeTypeAdapterFactory中会出现NPE异常。扩展上面的原始示例:

static class Wallet {
    BillingInstrument payment;
}

Wallet wallet = new Wallet();
// wallet.payment = new Card("Jo", 123); // leave wallet.payment uninitialized.

gson.toJson(wallet); // throws NPE
如果我初始化wallet.payment,则序列化工作正常。以下是堆栈跟踪:

Exception in thread, java.lang.NullPointerException
at com.me.test.RuntimeTypeAdapterFactory$1.write(RuntimeTypeAdapterFactory.java:218)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:91)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:206)
at com.google.gson.Gson.toJson(Gson.java:595)
...
以下几点:

有人遇到过这个问题并找到了解决办法吗?默认情况下,Gson应该忽略序列化空值,因此不确定在我的示例中它为什么尝试序列化wallet.payment

谢谢

遇到了同样的问题。 此修复对我有效:

(RuntimeTypeAdapterFactory.java)

@Override public void write(JsonWriter out,R值)引发IOException{
if(值!=null){
类srcType=value.getClass();
String label=submittolabel.get(srcType);
@SuppressWarnings(“unchecked”)//注册要求子类型扩展T
TypeAdapter委托=(TypeAdapter)子类型ToDelegate.get(srcType);
if(委托==null){
抛出新的JsonParseException(“无法序列化”+srcType.getName()
+“您忘记注册子类型了吗?”);
}
JsonObject JsonObject=delegate.toJsonTree(value.getAsJsonObject();
if(jsonObject.has(typeFieldName)){
抛出新的JsonParseException(“无法序列化”+srcType.getName()
+因为它已经定义了一个名为“+typeFieldName”的字段;
}
JsonObject clone=新的JsonObject();
添加(typeFieldName,新的JsonPrimitive(标签));
对于(Map.Entry e:jsonObject.entrySet()){
clone.add(e.getKey(),e.getValue());
}
写入(克隆,输出);
}否则{
out.nullValue();
}
在版本2.4之前的版本中,此问题已得到修复

           @Override public void write(JsonWriter out, R value) throws IOException {
            if(value!=null) {
                Class<?> srcType = value.getClass();
                String label = subtypeToLabel.get(srcType);
                @SuppressWarnings("unchecked") // registration requires that subtype extends T
                        TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType);
                if (delegate == null) {
                    throw new JsonParseException("cannot serialize " + srcType.getName()
                            + "; did you forget to register a subtype?");
                }
                JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject();
                if (jsonObject.has(typeFieldName)) {
                    throw new JsonParseException("cannot serialize " + srcType.getName()
                            + " because it already defines a field named " + typeFieldName);
                }
                JsonObject clone = new JsonObject();
                clone.add(typeFieldName, new JsonPrimitive(label));
                for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
                    clone.add(e.getKey(), e.getValue());
                }
                Streams.write(clone, out);
            }else{
                out.nullValue();
            }