Is there a JSON library that can serialize Proxy objects?

Posted by gmoore on Stack Overflow See other posts from Stack Overflow or by gmoore
Published on 2010-04-28T19:48:08Z Indexed on 2010/04/28 22:17 UTC
Read the original article Hit count: 254

Filed under:
|
|
|
|

Using ActiveObjects as my ORM and Gson as my JSON processor.

Ran into a problem going toJson from persisted objects. The problem is that my persisted class is actually an Interface and AO is proxying that object under the hood. Here's some sample code:

    Venue venue = manager.get(Venue.class, id);
    gson.toJson(venue);

Comes up with this exception:

java.lang.UnsupportedOperationException: Expecting parameterized type,
got interface java.lang.reflect.InvocationHandler.
 Are you missing the use of TypeToken idiom?
 See http://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and...

Because venue.getClass().getName() gives:

$Proxy228

I've tried a few solutions in various combinations:

    gsonBuilder.registerTypeAdapter(Venue.class, newVenueSerializer());
    Type listType = new TypeToken<Venue>() {}.getType();

Nothing has worked so far and I'm using a wonky field-by-field workaround. Any suggestions? I'm not married to Gson, so if there's an alternative library that can do this I'd be happy to use it.

© Stack Overflow or respective owner

Related posts about java

Related posts about activeobjects