FasterXML / FasterXML/jackson-future-ideas

Make @JsonSubTypes optional

Offen
#18 0 Kommentare 7 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Keine Sprachdaten
Sterne
21
Forks
3
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

```
@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

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.