Polymorphic JSON deserialization with Jackson
还没有人认领这个 Issue。
评估
调研方向
先从 SDK Java JSON-Jackson 文档以及 issue 中展示的 mapData 和 PojoCloudEventDataMapper 用法开始,然后将其与 Jackson 使用 @JsonTypeInfo 和 @JsonSubTypes 的方式进行比较。完成的标准是:提供一种已文档化或已实现的方式,用于从 CloudEvent 信封解析多态 payload 类型,并明确说明支持的行为和限制。
由索引模型根据 Issue 内容生成。
描述
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:
PojoCloudEventData<User> cloudEventData = mapData(
inputEvent,
PojoCloudEventDataMapper.from(objectMapper,User.class)
);
But in my case, I would like to achieve something like the following:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type"
)
@JsonSubTypes({
@JsonSubTypes.Type(value = Foo.class, name = "foo"),
@JsonSubTypes.Type(value = Bar.class, name = "bar")
})
sealed interface Payload permits Foo, Bar {}
record Foo(String str) implements Payload {}
record Bar(int i) implements Payload {}
// ...
PojoCloudEventData<Payload> cloudEventData = mapData(
inputEvent,
PojoCloudEventDataMapper.from(objectMapper, Payload.class)
);
Running this code, it results in an error (as expected):
com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, class Payload]: missing type id property 'type'
Because the type field is part of the CloudEvent header and/or envelope that Jackson does not know about at this point.
What's your take on this?
- 主要语言
- Java
- 星标
- 446
- 派生
- 172
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
cloudevents/sdk-java 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
cloudevents/sdk-java#731 · 1 个 reaction ·
-
bug
难度 1/5 1-3 小时 新手友好度 88/100
cloudevents/sdk-java#697 · 3 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 72/100
cloudevents/sdk-java#689 · 2 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 65/100
cloudevents/sdk-java#660 · 2 条评论 ·
-
难度 4/5 3-5 天 新手友好度 38/100
cloudevents/sdk-java#748 ·
查看 cloudevents/sdk-java 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 88/100
checkstyle/checkstyle#21755 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复
-
agentic-workflows
难度 2/5 1-3 小时 新手友好度 68/100
github/copilot-sdk#2782 ·
维护者通常 1 天内回复
-
documentation Good for newcomer quick win
难度 2/5 1-3 小时 新手友好度 68/100
CodeForPhilly/benefit-decision-toolkit#519 ·
维护者通常 1 天内回复
-
area-deployment triage:bot-seen triage:needs-human
难度 2/5 1-3 小时 新手友好度 68/100
microsoft/aspire#20533 · 1 条评论 ·
维护者通常 1 天内回复