question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:open
  • Created 6 years ago
  • Reactions:17
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

23reactions
jinmeiliaocommented, Apr 1, 2019

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 allow Cat cat = new ObjectMapper().readValue("{ \"sound\": \"mew\" }", Cat.class); to not throw the exception.

4reactions
jinmeiliaocommented, Mar 30, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found