2

I'm getting an error trying to deserialize what I believe is a valid JSON string:

String json = "{\"email\":\"testing@example.com\",\"password\":\"12345\"}";

// FlexJSON deserializer
JSONDeserializer<Signin> deserializer = new JSONDeserializer<Signin>();

// Deserialize into a Signin POJO.
Signin signin = deserializer.deserialize(json);

When I run this code, I get:

java.util.HashMap cannot be cast to com.myapp.server.Signin
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.myapp.server.Signin
    at com.myapp.server.SigninService.doPost(SigninService.java:39)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    ... rest of stack trace omitted for brevity

Is my JSON malformed? It's almost as if the JSON is somehow "bad" and FlexJSON is treating it like a HashMap...

IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
  • 2
    Your json is fine. It's your *CODE*: JSON-encoded strings have no inherent object type, so they becomes a generic hashmap when decoded. So exactly as the error says - the hashmap produced by deserializing cannot be cast to a `Signin` object. – Marc B Dec 24 '13 at 15:00
  • This is not a pure JSON question. The JSON is valid, the question is how FlexJSON converts the JSON object into an instance of `Signin`. – Koraktor Dec 24 '13 at 15:01
  • Thanks @MarcB (+1) - so how can I force FlexJSON to map the String back to a `Signin` instance? – IAmYourFaja Dec 24 '13 at 15:02
  • @CalifornianAcorn Have a look at [Genson](http://code.google.com/p/genson/), it has powerfull features and supports all kind of generic types – eugen Dec 24 '13 at 15:45

2 Answers2

4

Looking at the documentation, the problem is that your json doesn't declare its class. This means that you explicitly need to supply a Class object to the deserializer, as in Java generics are only compile time, not runtime.

To quote from the documentation:

We need to replace the type information we just dropped when we instantiate the deserializer. To do that we'll pass the class we want to use into to flexjson.JSONDeserializer.deserialize(String, Class) method like so:

Hero hero = new JSONDeserializer<Hero>().deserialize( jsonHarvey, Hero.class ); 

So use:

Signin signin = deserializer.deserialize(json, Signin.class);
Community
  • 1
  • 1
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
1

This library you're using seems like it's not supported really.
Seems it's not changed since 2010. I would try using something else.

I have used this one for example without any issues.
http://code.google.com/p/google-gson/downloads/list

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • It could just work and there was no need to release a new version, and according to [sourceforge](http://sourceforge.net/projects/flexjson/), the last update (a commit) was August 2013 – Mark Rotteveel Dec 24 '13 at 15:05
  • 1
    Thanks @peter.petrov (+1) - but I actually switched over to FlexJSON from GSON, because most of my POJOs have generic Set and List properties, and GSON is really lousy at deserializing generic types correctly... but if all else fails I'll use GSON for this one use case. In the meantime, I'd be interested to see if anyone can get me up and running with FlexJSON. Thanks again! – IAmYourFaja Dec 24 '13 at 15:05
  • @MarkRotteveel That's true. I am not sure. This was just my initial guess here based on experience. I try not to use libraries which no one maintains/changes for years. – peter.petrov Dec 24 '13 at 15:06
  • @CalifornianAcorn Then see Mark Rotteveel's answer. Seems he got it. – peter.petrov Dec 24 '13 at 15:07