I searched a lot and just find questions about polymorphic deserialization on the content inside the map. Is polymorphic deserialization of the card itself possible?
For example, I have a Book class containing Map as a member variable.
public class Book {
@JsonProperty
private Map<String, Object> reviews;
@JsonCreator
public Book(Map<String, Object> map) {
this.reviews = map;
}
}
Another class has a list of the class Book.
public class Shelf {
@JsonProperty
private List<Book> books = new LinkedList<>();
public void setBooks(List<Book> books) {
this.books = books;
}
public List<Book> getBooks() {
return this.books;
}
}
And a test class. One book review map is a Hashtable, and another book review map is a HashMap.
public class Test {
private Shelf shelf;
@BeforeClass
public void init() {
Map<String, Object> review1 = new Hashtable<>();
review1.put("test1", "review1");
Map<String, Object> review2 = new HashMap<>();
review2.put("test2", "review2");
List<Book> books = new LinkedList<>();
books.add(new Book(review1));
books.add(new Book(review2));
shelf = new Shelf();
shelf.setBooks(books);
}
@Test
public void test() throws IOException{
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
String json = mapper.writeValueAsString(shelf);
System.out.println(json);
Shelf sh = mapper.readValue(json, Shelf.class);
for (Book b : sh.getBooks()) {
System.out.println(b.getReviews().getClass());
}
}
}
Test output
{
"name" : "TestShelf",
"books" : [ {
"reviews" : {
"test1" : "review1"
}
}, {
"reviews" : {
"test2" : "review2"
}
} ]
}
class java.util.LinkedHashMap
class java.util.LinkedHashMap
Serialization works fine. But after deserialization, both overview1 and review2 are LinkedHashMap. I want review1 and review2 to be their real types, which are Hashtable for viewing1 and HashMap for viewing2. Is there any way to achieve this?
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);, json json. , . .