首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gson RuntimeTypeAdapterFactory -要序列化的空项目导致空指针异常

Gson RuntimeTypeAdapterFactory -要序列化的空项目导致空指针异常
EN

Stack Overflow用户
提问于 2015-01-04 05:57:42
回答 2查看 994关注 0票数 1

我正在测试RuntimeTypeAdapterFactoryTest:

https://code.google.com/p/google-gson/source/browse/trunk/extras/src/test/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactoryTest.java

它在原始示例测试用例(testRuntimeTypeAdapter())中工作得很好:

https://code.google.com/p/google-gson/source/browse/trunk/extras/src/test/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactoryTest.java#27

但是如果注册的类型是空的,我会在RuntimeTypeAdapterFactory中得到一个NPE异常。扩展上面的原始示例:

代码语言:javascript
复制
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,那么序列化就能正常工作。下面是堆栈跟踪:

代码语言:javascript
复制
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)
...

这一点如下:

https://code.google.com/p/google-gson/source/browse/trunk/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java#218

有没有人遇到过这种情况并找到了解决办法?默认情况下,Gson应该忽略序列化null值,所以我不确定为什么在我的示例中它会尝试序列化wallet.payment。

谢谢

EN

回答 2

Stack Overflow用户

发布于 2019-04-28 03:28:27

此问题已在2.4版之前的this commit中得到修复

票数 1
EN

Stack Overflow用户

发布于 2015-04-20 17:28:46

遇到了同样的问题。这个修复方法对我很有效:

(RuntimeTypeAdapterFactory.java)

代码语言:javascript
复制
           @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();
            }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27759968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档