FasterXML / FasterXML/jackson-future-ideas

Make @JsonSubTypes optional

Aperta
#18 0 commenti 7 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Nessun dato sulla lingua
Stelle
21
Fork
3
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

```
@JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
public abstract class Animal {
private String sound;
private String type;

public String getSound() {
return sound;
}

public void setSound(String sound) {
this.sound = sound;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}
}

@JsonTypeName("dog")
public class Dog extends Animal {
}

@JsonTypeName("elephant")
public class Elephant extends Animal {
}
```
When I try to deserialize json
```
{
"sound": "auh",
"type": "dog"
}
```
I get exception
`com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'dog' into a subtype of [simple type, class Animal]: known type ids = [Animal]`
But if I add @JsonSubType
```
@JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({@JsonSubTypes.Type(Dog.class), @JsonSubTypes.Type(Elephant.class)})
public abstract class Animal {
private String sound;
private String type;

public String getSound() {
return sound;
}

public void setSound(String sound) {
this.sound = sound;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}
}
```
this will work, so I propose by default to scan class children and add them as subtypes without annotation, or to make a module that does such thing

https://github.com/FasterXML/jackson-databind/issues/1715

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.