FasterXML / FasterXML/jackson-future-ideas
Make @JsonSubTypes optional
- Dominant language
- No language data
- Stars
- 21
- Forks
- 3
- PR merge metrics
- No merged PRs in 30d
Description
```
@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
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.