FasterXML / FasterXML/jackson-databind

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

オープン
#2,005 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る
polymorphic-handling
主要言語
Java
スター
3.7k
フォーク
1.5k
平均マージ
3日 6時間
マージ済み PR(30日)
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 を短くまとめたダイジェスト。