I serialize data in server:
Gson gson = new Gson();
Map<String, List<?>> resultMap = BackendUpdateManager.getInstance()
.getUpdates(timeHolder, shopIdInt, buyerIdInt);
gson.toJson(resultMap);
and deserialize:
Gson gson = new Gson();
Map<String, List<?>> resultMap = gson.fromJson(json,
new TypeToken<Map<String, List<?>>>() {
}.getType());
However, when I try use items from the Map
:
List<ProductCategory> productCategoryList = (List<ProductCategory>)updateMap.get(key);
for(ProductCategory productCategory : productCategoryList) {
}
I get error:
Caused by:
java.lang.ClassCastException
:com.google.gson.internal.LinkedTreeMap
cannot be cast tocom.example.model.entity.ProductCategory
How can I fix this error or otherwise create a Map
with List<different classes>
?
I've tried creating classes with getters and setters, that contains List
s of different classes instead of Map<String, List<?>>
and use it for serialization and deserialization. But I'm looking for a better way.