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.

Add `MapperFeature.USE_BASE_TYPE_AS_DEFAULT_IMPL` to use declared base type as `defaultImpl` for polymorphic deserialization

See original GitHub issue

I use @JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTypeInfo.As.PROPERTY, property = "type") for interfaces and abstract classes and it works as expected.

Now I have a case where the JSON string does not contain the ‘type’ property (external interface) but I know the concrete class to which this JSON string should be mapped.

When I now use objectMapper.readValue(jsonString, ConcreteClass.class) then I get an exception that the ‘type’ property is missing. That’s bad because I tell Jackson that the ‘type’ is ‘ConcreteClass.class’ so I want that Jackson tolerates the missing ‘type’ property. In other words: Please use the given class as ‘defaultImpl’ (see JsonTypeInfo attribute defaultImpl) if no JsonTypeInfo defaultImpl attribute was set but a concrete class was given.

Or is there another way to define a ‘defaultImpl’ when using readValue()?

Thank you!

Example:

@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTypeInfo.As.PROPERTY, property = "type")
public interface MyInterface {
  String getName();
  void setName(String name);
}

public class MyClass implements MyInterface {
  private String name;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
}

This works:

{
  "name": "name",
  "type": ".MyClass"
}
objectMapper.readValue(jsonString, MyInterface.class);

This not (but it would be very nice if you can make it work):

{
  "name": "name"
}
objectMapper.readValue(jsonString, MyClass.class);

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:13
  • Comments:25 (14 by maintainers)

github_iconTop GitHub Comments

5reactions
cowtowncodercommented, Oct 6, 2015

You can specify “default type” with @JsonTypeInfo(defaultImpl=DefaultImplementClass.class)

As to whether it’d be possible to force use of actual sub-class… I don’t know off-hand. It is an interesting idea, and if that is possible to implement could make sense. But I’d have to see how code path works for this particular case; polymorphic type handling is quite specialized system.

2reactions
kopporcommented, Jun 27, 2017

Thank you for the hint regarding @JsonTypeInfo(defaultImpl=...). Here, it works perfectly when being used in subclasses and also solves my stackoverflow question: How to deserialize JSON having refrences to abstract types in Jackson.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using @JsonTypeInfo and @JsonSubTypes, Deserialize an ...
MapperFeature.USE_BASE_TYPE_AS_DEFAULT_IMPL is a feature that specifies whether the declared base type of a polymorphic value is to be used as ...
Read more >
MapperFeature (jackson-databind 2.14.1 API) - javadoc.io
Feature that specifies whether the declared base type of a polymorphic value is to be used as the "default" implementation, if no explicit...
Read more >
Jackson 2.10: Safe Default Typing - cowtowncoder - Medium
When deserializing, Jackson would notice that declared base type of “value” is MyValue and would call method PolymorphicTypeValidator.
Read more >
A brand new website interface for an even better experience!
Add `MapperFeature.USE_BASE_TYPE_AS_DEFAULT_IMPL` to use declared base type as `defaultImpl` for polymorphic deserialization.
Read more >
JsonTypeInfo (Jackson-annotations 2.5.0 API) - FasterXML
Class <?> defaultImpl. Optional property that can be used to specify default implementation class to use for deserialization if type identifier is either...
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