I'm using jackson to read and write object into local json file.
It successfully read and write value. But problem is when I read value from file. and assign it to my object it give me an error.
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.testing.FeedItem
I know the reason of error because to read value from file jackons use LinkedHashMap So, LinkedHashMap can not cast to object(FeedItem)
Here is what I'm using.
PostFeed
public class PostFeed{
private String name;
private List<FeedItem> feeditemList;
// other code getter setter etc
}Reading Value from File
ObjectMapper objectMapper = new ObjectMapper();
PostFeed postFeed = objectMapper.readValue(offlinePrivateFeed, PostFeed.class);
FeedItem feedItem = postFeed.getFeedList().get(0); // ErrorUpdate Write Value to local File
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValue(Utils.getOutputJsonFile("private-feed"), postFeed);I found similar question LinkedhashMap can not cast to model Account But this question is not useful in my case, Almost I tried all answer but no one solve my problem.
Can you please let me know how can I solve this ?
Update: FeedItem
public class FeedItem{
private String name;
private int badge;
private Boolean allow;Update: JSON File
{
"feedName": "test123",
"time" : "7-7-1994",
"feedList": [
{
"name": "feedItemName",
"badge": 4,
"allow": false
},
{
"name": "feedItemName",
"badge": 1,
"allow": true
},
{
"name": "feedItemName",
"badge": 2,
"allow": false
}
]
}implementation 'com.fasterxml.jackson.core:jackson-databind:2.6.3'
public final class FeedItem extends GenericJson {
@Key private String name;
@Key private int badge;
@Key private Boolean allow;
public FeedItem setName(String name){
this.name = name;
return this;
}
public String getName(){
return this.name;
}
// other getter and setter
}
GenericJson is from Cloud endpoint lib, It's a complex class and I don't know what it is.
Is this problem is due to such kind of class ?
Cloud endpoint libs automatically convert JSON data into objects using jackson2 that's mean it's possible by using this.
But how I can do it my self ?