1

I used the JsonTypeInfo to correctly deserialize the json to appropriate sub-types

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
public abstract class Car {

}

public class Audi extends Car {
   // Getters and Setters
}

public class Merc extends Car {
   // Getters and Setters
}

For the following structure - Map<String, Car> info

Here is my sample JSON

{
   info: {
      "xyz": {
         type: "Audi"
      },
      "abc": {
         type: "Merc" 
      }
   }
}

The above JSON does not deserialize correctly and get the error 400: Unable to process JSON. What am I missing here ?

Manjunath Manoharan
  • 4,567
  • 6
  • 28
  • 43
  • possible "duplicate" of https://stackoverflow.com/questions/8565299/polymorphism-in-json-jersey-and-jackson?rq=1 – Nico Jul 21 '17 at 08:47

1 Answers1

0

Two things, the JSON is incorrect because the properties needs to be between quotes:

{
   "info": {
      "xyz": {
         "type": "Audi"
      },
      "abc": {
         "type": "Merc" 
      }
   }
} 

And the second, try to add this annotation to the abstract class:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({ 
    @JsonSubTypes.Type(value = Audi.class, name = "Audi"), 
    @JsonSubTypes.Type(value = Merc.class, name = "Merc")})
abstract class Car {

It basically indicates the subtypes and the names associated.

Franjavi
  • 647
  • 4
  • 14