@JsonDeserialize(contentUsing=...) is ignored if content type is determined by @JsonTypeInfo
See original GitHub issueTested versions: 2.9.0.pr3, 2.8.8 and 2.0.1
Expected behavior: Jackson respects @JsonDeserialize(contentUsing=...)
even if the content’s type is determined via @JsonTypeInfo
.
Actual behavior: @JsonDeserialize(contentUsing=...)
is ignored.
The following example demonstrates the issue. The expected output is “ok”. The observed output is empty. BarDeserializer is never called (nor constructed for that matter).
Removing the @JsonTypeInfo
line “fixes” the example.
public class Test {
static class BarDeserializer extends JsonDeserializer<Bar> {
@Override
public Bar deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
System.out.println("ok");
return p.readValueAs(Bar.class);
}
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
static class Bar {
public int dummy;
}
static class Foo {
@JsonDeserialize(contentUsing = BarDeserializer.class)
public List<Bar> bars;
}
public static void main(String[] args) throws IOException {
Foo foo = new Foo();
foo.bars = Arrays.asList(new Bar());
ObjectMapper om = new ObjectMapper();
om.readValue(om.writeValueAsString(foo), Foo.class);
}
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:14 (6 by maintainers)
Top Results From Across the Web
Developers - @JsonDeserialize(contentUsing=...) is ignored if ...
Expected behavior: Jackson respects @JsonDeserialize(contentUsing=...) even if the content's type is determined via @JsonTypeInfo . Actual behavior: @ ...
Read more >java - Deserialize Custom Collection with abstract Class Content
i have the following Problem: System: public class System { @JsonDeserialize(converter = MyDeserializer.class) ObservableList<AClass> ...
Read more >ObjectMapper (jackson-databind 2.13.2 API) - Javadoc.io
Method to deserialize JSON content from given resource into given Java type. <T> T, readValue(URL src, JavaType valueType). Same as readValue ...
Read more >com.fasterxml.jackson.databind.introspect ...
The class is part of the package ➦ Group: com.fasterxml.jackson.core ➦ Artifact: ... ignore primitive<->wrapper refinements contentType = contentType.
Read more >Index (jackson-databind 2.6.0 API) - FasterXML
Method called to create (or, for completely immutable deserializers, reuse) a deserializer that can convert JSON content into values of specified Java type....
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 Free
Top 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
@james-woods I ended up with something like the following:
For my use case, registering a list of polymorphic typed objects so they could be referenced by constructors from objects later in the same json file, this was sufficient.
Yeah I worked around it by writing a custom deserializer for the entire list, which is basically a single line of extra code.
Perhaps this behavior could be documented in the mean time, because it isn’t immediately obvious (to me at least) why contentUsing isn’t compatible with polymorphic types.