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
Polymorphic JSON deserialization with Jackson #666
I was wondering how to write a polymorphic parser for JSON CloudEvents, where the payload can be one of many types.
Normally, with plain Jackson one can do something like this, using @JsonTypeInfo and a type field somewhere in the JSON to determine the specific class for deserialization.
In this simple example in the CloudEvent SDK docs we knew the type in advance:
@mbechto in previous versions, while using the structure mode with access to the object mapper, this could be achieved easily with a mixin by adding an external behavior to the cloudevents class.
I believe the same could be used somehow, but to be honest, i am having a hard time navigating PojoCloudEventData its Mapper and escipally CloudEvent complete excluding any generic type. Maybe could someone share the reasoning, i am sure there was.
For Mix in approach (pay attention on the JsonTypeInfo.As.EXTERNAL_PROPERTY):
the mixin interface:
publicinterfaceCloudEventMixin<T> {
@JsonTypeIdStringgetType();
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
property = "type",
defaultImpl = Void.class)
@JsonSubTypes({
// @JsonSubTypes.Type(value = Foo.class, name = "fulltypenameespace.foo")})
TgetData();
}
So was just to register this interface to the CloudEvents interface, in this case we could do the same with the pojo, but i believe the sdk could help more here :(
Activity
gustavomonarin commentedon Nov 1, 2024
@mbechto in previous versions, while using the structure mode with access to the object mapper, this could be achieved easily with a mixin by adding an external behavior to the cloudevents class.
I believe the same could be used somehow, but to be honest, i am having a hard time navigating PojoCloudEventData its Mapper and escipally CloudEvent complete excluding any generic type. Maybe could someone share the reasoning, i am sure there was.
For Mix in approach (pay attention on the JsonTypeInfo.As.EXTERNAL_PROPERTY):
the mixin interface:
So was just to register this interface to the CloudEvents interface, in this case we could do the same with the pojo, but i believe the sdk could help more here :(
I believe this could work if adjust your annotations to an interface targeting the
PojoCloudEventDatainstead of my CloudEvent focused.