RFE: allow missing of type id if target value is concrete
See original GitHub issue@JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({@JsonSubTypes.Type(value = Dog.class, name = "dog"), @JsonSubTypes.Type(value = Cat.class, name = "cat")})
public abstract class Animal {
private String sound;
public String getSound() {
return sound;
}
public void setSound(String sound) {
this.sound = sound;
}
}
public class Dog extends Animal {
}
public class Cat extends Animal {
}
public static void main(String[] args) throws IOException {
Animal animal = new ObjectMapper().readValue("{ \"type\": \"cat\", \"sound\": \"mew\" }", Animal.class); // everything ok
System.out.println(animal.getSound());
Cat cat = new ObjectMapper().readValue("{ \"sound\": \"mew\" }", Cat.class); // exception
System.out.println(cat.getSound());
}
}
Exception:
com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'type' that is to contain type id (for class Cat)
at [Source: { "sound": "mew" };
When json is mapped to parent class is ok to send “type” property, but when is mapped to child class, the “type” property must not be required.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:17
- Comments:11 (4 by maintainers)
Top Results From Across the Web
[RFE] Expose ipv6.addr-gen-mode option to the global config
It would be useful to set this value at the global level in order to give the system admin the ability to control...
Read more >Recursive Feature Elimination (RFE) for Feature Selection in ...
RFE is a wrapper-type feature selection algorithm. This means that a different machine learning algorithm is given and used in the core of...
Read more >Predicting missing proteomics values using machine learning
Predicting missing proteomics values using machine learning: Filling the gap using transcriptomics and other biological features.
Read more >A Program Annotation Facility for the Java Programming ...
This facility allows developers to define custom annotation types and to annotate ... int id(); // Unique ID number associated with RFE String...
Read more >feature_engine Documentation - Read the Docs
Feature-engine allows you to select the variables you want to transform within each transformer. This way, different engineering procedures can ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I actually found a way to achieve this. There is a MapperFeature that already exists for this:
mapper.configure(MapperFeature.USE_BASE_TYPE_AS_DEFAULT_IMPL, true);
this will allowCat cat = new ObjectMapper().readValue("{ \"sound\": \"mew\" }", Cat.class);
to not throw the exception.Can I request this feature to be added? If I specifically asked the json string to be parsed into the concrete type
Cat cat = new ObjectMapper().readValue("{ \"sound\": \"mew\" }", Cat.class);
, it would be nice that I don’t need to include the type info in the json itself.