FasterXML / FasterXML/jackson-databind

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

Ouverte
#2,005 3 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
polymorphic-handling
Langage dominant
Java
Étoiles
3.7k
Forks
1.5k
Merge moyen
3 j 6 h
PR mergées (30 j)
28

Description

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?

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.