FasterXML / FasterXML/jackson-databind

Polymorphic serialization: use property name directly as type info, without wrapper

未关闭
#2,005 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
polymorphic-handling
主要语言
Java
星标
3.7k
派生
1.5k
平均合并
3 天 6 小时
30 天内合并 PR
28

描述

Given:

public class Zoo {
private Animal animal;
}
public abstract class Animal {
private String name = "Benji";
}
public class Dog extends Animal {
}
public class Cat extends Animal {
}

I want to be able to serialize an instance of `Zoo` so that it outputs either:

{
"dog": {
"name": "Benji"
}
}

or

{
"cat": {
"name": "Benji"
}
}

depending on the concrete type of `animal` in `Zoo`.

As far as I can tell, none of the options `@JsonTypeInfo` currently support this. The closest I can get is with the default `@JsonTypeInfo.As.WRAPPER_OBJECT`, but that results in:

{
"animal":
"cat": {
"name": "Benji"
}
}
}

I really don't want to have the wrapper object. The reason is that I want to remain backwards compatible with a previous incarnation of my object model where there were only `Dog`s, the `Animal` abstraction hadn't been created, and `Animal animal` in `Zoo` was `Dog dog`.

Is it possible to add a sub-type (de)serialization option that implements this?

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。