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.

Deserializing nested polymorphic types does not work

See original GitHub issue
@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY,
        property = "type")
@JsonSubTypes({
        @JsonSubTypes.Type(value = Car.class, name = "car"),
        @JsonSubTypes.Type(value = Truck.class, name = "truck")
})
public abstract class Vehicle {
    protected String make;
    protected String model;
    protected String type;

    // no-arg constructor, getters and setters
}
public class Truck extends Vehicle {
    private double payloadCapacity;

     // no-arg constructor, getters and setters
}
@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY,
        property = "status")
@JsonSubTypes({
        @JsonSubTypes.Type(value = CarLite.class, name = "lite"),
        @JsonSubTypes.Type(value = CarWeight.class, name = "weight")
})
public class Car extends Vehicle {
    protected int seatingCapacity;
    protected double topSpeed;
    protected String status;

     // no-arg constructor, getters and setters
}
public class CarLite extends Car {
    private String lite;

     // no-arg constructor, getters and setters
}
public class CarWeight extends Car {
    private int weight;

    // no-arg constructor, getters and setters
}
public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();

        String json = "[\n" +
                "        {\n" +
                "            \"type\": \"car\",\n" +
                "            \"make\": \"Mercedes-Benz\",\n" +
                "            \"model\": \"S500\",\n" +
                "            \"seatingCapacity\": 5,\n" +
                "            \"topSpeed\": 250.0,\n" +
                "            \"status\": \"lite\",\n" +
                "            \"lite\": \"#ccc\"\n" +
                "        },\n" +
                " \n" +
                "        {\n" +
                "            \"type\": \"truck\",\n" +
                "            \"make\": \"Isuzu\",\n" +
                "            \"model\": \"NQR\",\n" +
                "            \"payloadCapacity\": 7500.0\n" +
                "        }\n" +
                "    ]";

        List<Vehicle> v = objectMapper.readValue(json, new TypeReference<List<Vehicle>>() {});

        v.forEach(System.out::println);
    }
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "lite" (class models.Car), not marked as ignorable (6 known properties: "topSpeed", "seatingCapacity", "model", "type", "make", "status"])

Using jackson 2.9.5

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
pedrovinivcommented, May 3, 2020

@raderio Hi. I know it’s been a couple of time. But I was facing the same issue and I developed a custom JsonDeserializer to workaround this limitation. The idea is to support multiple discriminator fields when deserializing the JSON. checkout the repository: https://github.com/pedroviniv/multi-discriminators-polymorphic-deserializer

0reactions
cowtowncodercommented, Dec 30, 2018

@raderio I am sure other libraries can implement it. I have no interest in implementing, or using my limited time on documenting why I think it is not worth the hassle. If you truly want to figure it out that’s fine of course. It may be fun exercise.

One starting point would be figuring out how polymorphic deserialization actually works. JsonDeserializer and TypeDeserializer are main abstractions that handle interaction between “regular” deserializer (which handles content) and type deserializer that handles extracting of type id from json content to figure out which content deserializer to call.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson deserializing nested polymorphic type - Stack Overflow
I get the standard exception about abstract types. org.codehaus.jackson.map.JsonMappingException: Can not construct instance of com.adfin.
Read more >
Nested Polymorphic Deserializer using JSON.Net and Generics
First Attempt. My first attempts to deserialize were complete failures; The results would come back as a single UISchema object with no elements ......
Read more >
Polymorphic serialization for object types - .NET
System.Text.Json has historically hardcoded polymorphism for root-level object values but not for nested object values. Starting with .NET 7, ...
Read more >
Serialization for Nested Polymorphic Types with FasterXML ...
Hi there, I got into a serialization issue with nested polymorphic types as described below. Looks like the @JsonTypeInfo annotation marked on ...
Read more >
Recursive Polymorphic Deserialization with System.Text.Json
Goal: Deserialize a nested JSON structure, where the objects instantiated are not instances of the given abstract base type but rather are ...
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