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.

@JsonDeserialize(contentUsing=...) is ignored if content type is determined by @JsonTypeInfo

See original GitHub issue

Tested 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:open
  • Created 6 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
pdegoejecommented, Sep 7, 2017

@james-woods I ended up with something like the following:

  static class BarListDeserializer extends JsonDeserializer<List<Bar>> {
    @Override
    public List<Bar> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
      List<Bar> barList = jsonParser.readValueAs(new TypeReference<List<Bar>>() { });
      // do post-processing
      return barList;
    }
  }

  static class Foo {
    // Use a custom deserializer for the entire list, instead of a per element deserializer.
    @JsonDeserialize(using = BarListDeserializer.class)
    public List<Bar> bars;
  }

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.

1reaction
pdegoejecommented, Jun 13, 2017

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.

Read more comments on GitHub >

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

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