java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.testing.FeedItem

15,092 views
Skip to first unread message

Azeem Haider

unread,
Jun 27, 2018, 12:26:51 PM6/27/18
to jackson-user

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); // Error


Update 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 ?

Tatu Saloranta

unread,
Jun 27, 2018, 12:30:39 PM6/27/18
to jackson-user
This is not a full reproduction of the issue as it is missing
definition of `FeedItem`, or input you are using.
What exception suggests is that somewhere type of entries in
`feeditemList` are not declared as `FeedItem`, but as
`Object` (or generic type like `?` from `List<?>`).

So full reproduction with everything needed to get the exception would
be needed.

-+ Tatu +-
> --
> You received this message because you are subscribed to the Google Groups
> "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jackson-user...@googlegroups.com.
> To post to this group, send email to jackso...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Azeem Haider

unread,
Jun 27, 2018, 11:54:18 PM6/27/18
to jackson-user
Here is FeedItem


Update: FeedItem

  public class FeedItem{
     private String name;
     private int badge;
     private Boolean allow;

Azeem Haider

unread,
Jun 28, 2018, 2:08:08 AM6/28/18
to jackson-user

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
  }

  ]

}

Tatu Saloranta

unread,
Jun 28, 2018, 12:59:47 PM6/28/18
to jackson-user
Ok, as far as I know and can see, that should work. This assuming that
all setters (or, if used, fields) have proper types.

If it does not work for you, I'd file an issue with full reproduction
(ideally as a unit test), indicating which Jackson version this is
with.
And without external dependencies like Spring or Lombok.

-+ Tatu +-

Azeem Haider

unread,
Jun 28, 2018, 11:38:13 PM6/28/18
to jackson-user
I'm using Goolge Cloud Endpoint v2. FeedItem class is auto generated by endpoint library. 
And I'm using google client libs which include jackson2. 

But for data-bind I'm using this.

implementation 'com.fasterxml.jackson.core:jackson-databind:2.6.3'

I already post an issue right here https://github.com/FasterXML/jackson-databind/issues/2075  But no reply.

How auto generated FeedItem Actually look like. 

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 ?

Tatu Saloranta

unread,
Jul 6, 2018, 1:00:52 PM7/6/18
to jackson-user
On Thu, Jun 28, 2018 at 8:38 PM, Azeem Haider <axeem...@gmail.com> wrote:
> I'm using Goolge Cloud Endpoint v2. FeedItem class is auto generated by
> endpoint library.
> And I'm using google client libs which include jackson2.
>
> But for data-bind I'm using this.
>
> implementation 'com.fasterxml.jackson.core:jackson-databind:2.6.3'

Ok. That is an old version, and updates are no longer provided. So one
thing to try is to use a more up-to-date
version; latest is 2.9.6, but 2.8.10 and even 2.7.9 have important
fixes that might be relevant.

>
> I already post an issue right here
> https://github.com/FasterXML/jackson-databind/issues/2075 But no reply.
>
> How auto generated FeedItem Actually look like.
>
> 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
>
> }
>

What does `@Key` mean? Does that add something via bytecode generation?

>
> 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 ?

I do not know based on just this information.

>
>
> 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 ?

By creating `ObjectMapper` and using `readValue()` method, giving
target type (usually Class),
passing json you want.
The only (?) caveat is that some frameworks add custom
JsonDeserializers for types they support,
and this may well be the source of problem you have.

-+ Tatu +-

rahul kumar

unread,
Dec 3, 2019, 1:48:17 PM12/3/19
to jackson-user
The ClassCastException thrown to indicate that your code has attempted to cast an object to a subclass of which it is not an instance.  Casting only works when the casted object follows an is a relationship to the type you are trying to cast to.

When will be ClassCastException is thrown:

  • When you try to cast an object of Parent class to its Child class type, this exception will be thrown.
  • When you try to cast an object of one class into another class type that has not extended the other class or they don't have any relationship between them.
Reply all
Reply to author
Forward
0 new messages