How do I configure Jackson ObjectMapper to deserialize all fields of type Foo into instances of SubclassOfFoo?

user82928

I'm deserializing a large json value. Deeply nested within that value is a json object like the following:

{
  "fieldOne": "valueOne",
  "fieldTwo": {
    "innerField": "innerValue"
  }
}

I'm using the Jackson ObjectMapper to deserialize the large json value into a 3rd party class. Deeply nested within that 3rd party class is another 3rd party class:

public class DeepThirdPartyClass {
    public String fieldOne;
}

which unfortunately is missing the fieldTwo property. I can create my own class which adds the missing field:

public class MyClass extends DeepThirdPartyClass {
    public MySubObject fieldTwo;
}

How do I configure jackson so that whenever it attempts to deserialize a value to DeepThirdPartyClass, it deserializes to MyClass instead?

user82928

I reworked @olga-khylkouskaya's solution to fit my problem:

@Test
public void newDeserializer() throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    SimpleModule module = new SimpleModule("DeepThirdPartyClass subclass override", new Version(1, 0, 0, "FINAL", "com.example", "deep-third-party-class-override"));
    module.addDeserializer(DeepThirdPartyClass.class, new JsonDeserializer<DeepThirdPartyClass>() {
        @Override
        public DeepThirdPartyClass deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            return p.readValueAs(MyClass.class);
        }
    });
    objectMapper.registerModule(module);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    String json = "{\n" +
            "  \"middle\": {\n" +
            "    \"fieldOne\": \"valueOne\",\n" +
            "    \"fieldTwo\": {\n" +
            "      \"fieldThree\": \"valueThree\"\n" +
            "    }\n" +
            "  }\n" +
            "}\n";

    ThirdPartyClass thirdPartyClass = objectMapper.readValue(json, ThirdPartyClass.class);
}

public class ThirdPartyClass {
    public DeepThirdPartyClass middle;
}

public class InnerClass {
    public String fieldThree;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I access the underlying Jackson ObjectMapper in REST Assured?

How do I create an object from a JSON string using Jackson's ObjectMapper?

How to deserialize interface fields using Jackson's objectMapper?

How do I correctly reuse Jackson ObjectMapper?

How do I deserialize timestamps that are in seconds with Jackson?

Jackson ObjectMapper cannot deserialize POJO, throws an exception: No suitable constructor found for type [...]: can not instantiate from JSON object

How to deserialize Spring's ResponseEntity with Jackson ObjectMapper (probably with using @JsonCreator and Jackson mixins)

How do I parse a Mongodb's UUID object using jackson ObjectMapper?

How to deserialize null type JSON fields with Jackson

Spring Boot JUnit Jackson Cannot Deserialize All Fields

How to deserialize a generic type with ObjectMapper Jackson

How to configure Jackson ObjectMapper for Camel in Spring Boot

How do I deserialize object variables using Jackson?

Reuse Jackson ObjectMapper and JsonFactory instances

How do I deserialize JSON into a List<SomeType> with Kotlin + Jackson

How do I deserialize into trait, not a concrete type?

How does Jackson deserialize json into a generic type?

How to deserialize with jackson objectmapper

How to configure Jackson to be able to deserialize special collections?

Jackson ObjectMapper: How to omit (ignore) fields of certain type from serialization?

How do I deserialize an array with indexes using jackson

How to replace null fields (nested at all levels) from JSON response using Jackson ObjectMapper serialization?

How do I tell Jackson ObjectMapper to use the actual properties instead of camel casing them?

Jackson ObjectMapper deserialize object with array

How do I configure Jackson Serialization on LocalDateTime and LocalDate for Java?

How do I deserialize this type of data structure

How do I use Jackson Databind to deserialize the following JSON?

How can I configure Jackson to write all fields of a Kotlin data class that implements a collections interface?

How do I get all instances of HasField type class?