Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.PROPERTY, property = "_name")
  2. @JsonSubTypes({@Type(value = Lion.class, name = "lion"), @Type(value = Elephant.class, name = "elephant")})
  3. public abstract class Animal {...}
  4.  
  5. public class Zoo {
  6. List<Animal> animals;
  7. }
  8.  
  9. {"animals":[{"_name":"lion"},{"_name":"elephant"}]}
  10.  
  11. {"animals":[{"_name":"lion"},{"_name":"elephant"},{"_name":"Turtle"}]}
  12.  
  13. List<Animal> animals = Lists.newArrayList(lion, elephant);
  14. Zoo zoo = new Zoo(animals);
  15. ObjectMapper mapper = new ObjectMapper();
  16. String zooString = mapper.writeValueAsString(zoo);
  17.  
  18. Zoo zooDeserialized = mapper.readValue(zooString, Zoo.class);
  19.  
  20. Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Could not resolve type id 'Turtle' into a subtype of [simple type, class mypackage.Animal]
  21. at [Source: N/A; line: -1, column: -1] (through reference chain: mypackage.Zoo["animals"])
  22. at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
  23. ..
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement