You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[bug] sub class field missing during serialization if collection's element type is a WildcardType #1870
List<? extends TestBeanBase> list1;
list1.add(new TestBeanSub()); // TestBeanSub is a child class of TestBeanBase
I find that the fields of TestBeanSub is missed in the serialized json.
You can try my example code:
@TestpublicvoidtestList() {
List<TestBeanBase> list = newArrayList<>();
//base classTestBeanBasebaseBean = newTestBeanBase();
baseBean.a = "1";
list.add(baseBean);
//sub classTestBeanSubsubBean = newTestBeanSub();
subBean.a = "1";
subBean.b = "2";//b is a field declared in sub class, this field value is missed during serializationlist.add(subBean);
TestBeanListbeanList = newTestBeanList();
beanList.list1 = list;
Gsongson = newGsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(beanList));
}
publicstaticclassTestBeanList {
publicList<? extendsTestBeanBase> list1; //element type is a WildcardType
}
publicstaticclassTestBeanBase {
publicStringa;
}
publicstaticclassTestBeanSubextendsTestBeanBase {
publicStringb;
}
Output json :
{
"list1": [
{
"a": "1"
},
{
"a": "1" //field b is missed
}
]
}
There is #170 which seems to exactly describe your problem, but it was merged into #231 despite that issue apparently covering a completely different use case.
Edit: No, that issue is probably unrelated because it does not involve usage of wildcards. Maybe that issue is from before TypeAdapterRuntimeTypeWrapper (or similar functionality) existed.
The underlying issue is that TypeAdapterRuntimeTypeWrapper.getRuntimeTypeIfMoreSpecific(Type, Object) (internal Gson method) does not choose the runtime type when a wildcard is used (which might be a bug?).
A workaround for this would be to create a custom TypeAdapterFactory which always uses the adapter for the runtime type:
This will decrease performance and might have unintended side effects when you actually want to use the adapter for the compile-time type in some cases.
There is #170 which seems to exactly describe your problem, but it was merged into #231 despite that issue apparently covering a completely different use case.
The underlying issue is that TypeAdapterRuntimeTypeWrapper.getRuntimeTypeIfMoreSpecific(Type, Object) (internal Gson method) does not choose the runtime type when a wildcard is used (which might be a bug?).
A workaround for this would be to create a custom TypeAdapterFactory which always uses the adapter for the runtime type:
This will decrease performance and might have unintended side effects when you actually want to use the adapter for the compile-time type in some cases.
Activity
Marcono1234 commentedon Mar 3, 2021
There is #170 which seems to exactly describe your problem, but it was merged into #231 despite that issue apparently covering a completely different use case.Edit: No, that issue is probably unrelated because it does not involve usage of wildcards. Maybe that issue is from before
TypeAdapterRuntimeTypeWrapper(or similar functionality) existed.The underlying issue is that
TypeAdapterRuntimeTypeWrapper.getRuntimeTypeIfMoreSpecific(Type, Object)(internal Gson method) does not choose the runtime type when a wildcard is used (which might be a bug?).A workaround for this would be to create a custom
TypeAdapterFactorywhich always uses the adapter for the runtime type:This factory would then have to be registered after all other factories and adapters on the
GsonBuilder:This will decrease performance and might have unintended side effects when you actually want to use the adapter for the compile-time type in some cases.
cnzhujie commentedon Mar 9, 2021
Thanks, it works for me
use the runtime type for a wildcard element type
use the runtime type for a wildcard element type (google#3112)