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
The class TypedDate is generated by the Immutables library.
Now suppose I have another abstract class that makes use of TypedDate but the field variable is defined as ITypedDate
When I am using the Gson object with the registered GsonTypeAdapter for NumberEvent and invoke toJson() then the Json representation for the 'dates' field is an empty list even though inspecting the NumberEvent object instance shows that there are typed dates present.
Some debugging inside Immutables led me to the gson.getTypeAdapter(ITypedDate.class) that does not return the generated Immutables type adapter.
When using
publicabstract@NullableList<TypedDate> dates();
instead of
publicabstract@NullableList<ITypedDate> dates();
everything works as expected.
Is there a way to force the type adapter to be recognized for the interface also?
Best regards,
Frank
The text was updated successfully, but these errors were encountered:
Hi, thank you for raising this! In this case Gson have no registered binding to the generated type adapter from ITypedDate. Only AbstractTypedDate and TypedDate have generated adapters bound. So we need to register the adapter to ITypedDate.
Just to give an idea, it might look something like this
On Fri, Sep 7, 2018 at 7:29 PM Eugene Lukash ***@***.***> wrote:
Hi, thank you for raising this! In this case Gson have no registered
binding to the generated type adapter from ITypedDate. Only
AbstractTypedDate and TypedDate have generated adapters bound. So we need
to register the adapter to ITypedDate.
Just to give an idea, it might look something like this
public static TypeAdapterFactory bridgeAdapter(final Class<?> to, final Class<?> from) {
return new TypeAdapterFactory() {
@SuppressWarnings("unchecked")
@OverRide public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (type.getRawType().equals(to)) {
return (TypeAdapter<T>) gson.getAdapter(from);
}
return null;
}
@OverRide public String toString() {
return "bridgeAdapter(to=" + to + ", from=" + from);
}
};
}
gsonBuilder.registerTypeAdapterFactory(bridgeAdapter(ITypedDate.class, TypedDate.class));
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#822 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AJKoL3_sjPB9R3O5YMP0n3fEVC_OhASnks5uYqzzgaJpZM4WegvL>
.
Hi,
I have a problem with GsonTypeAdapter when using an interface in a immutable class. Let me explain what I mean with an example:
Interface ITypedDate:
Abstract class AbstractTypedDate:
The class TypedDate is generated by the Immutables library.
Now suppose I have another abstract class that makes use of TypedDate but the field variable is defined as ITypedDate
Interface INumberEvent:
Abstract class AbstractNumberEvent:
When I am using the Gson object with the registered GsonTypeAdapter for NumberEvent and invoke
toJson()
then the Json representation for the 'dates' field is an empty list even though inspecting the NumberEvent object instance shows that there are typed dates present.Some debugging inside Immutables led me to the
gson.getTypeAdapter(ITypedDate.class)
that does not return the generated Immutables type adapter.When using
instead of
everything works as expected.
Is there a way to force the type adapter to be recognized for the interface also?
Best regards,
Frank
The text was updated successfully, but these errors were encountered: