1

i'm using the Restlet library for a WS server and i've recently switched from XStream/Jettison to Jackson as a JSON serializer/deserializer because of some issues.

A first drawback is that my ArrayList< Profile > (previously a Vector with Jettison) it doesn't wrap the list of Profiles when serialized and the JSON instead of "Profile:[{firstProfile}, {secondProfile}]" it looks like: [{firstProfile}, {secondProfile}]

I can overcome to this issue in the client telling manually which is the correct mapping but i would prefer to use a KVC approach.

I've looked around and it seems that it's a known issue: http://wiki.fasterxml.com/JacksonPolymorphicDeserialization (5.1 Missing type information on Serialization) that it suggest to:

  • Use arrays instead of Lists
  • Sub-class list, using class MyPojoList extends ArrayList { }
  • Force use of specific root type

the simplest way it should be to return an "Profile[] profile" array but it seems not working, before trying the other solutions i've rechecked around and it seems that you can use a @XmlRootElement(name = "Profile") to wrap the JSON root element: http://jira.codehaus.org/browse/JACKSON-163?focusedCommentId=213588&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-213588

so for using JAXB annotations with Jackson you need to configure the objectMapper: http://wiki.fasterxml.com/JacksonJAXBAnnotations

but in restlet to do so you need to override createObjectMapper to pass a Custom converter (see: http://restlet-discuss.1400322.n2.nabble.com/Set-custom-objectMapper-to-Jackson-Extension-td6287812.html and http://restlet-discuss.1400322.n2.nabble.com/Jackson-Mix-in-Annotations-td6211060.html#a6231831)

this is what i'm trying now! the question is there a more straightforward way to achieve this??

Thanks!!

kky
  • 141
  • 2
  • 13
  • I am so happy that Jackson is not using that crappy Foo:{key,value} notation .. – Heiko Rupp Jan 16 '12 at 16:05
  • it seems that in this example it's working: http://stackoverflow.com/questions/3954265/jackson-custom-collection-serialization-to-json the only differences is that i'm not using an interface and the restlet is taking care of the serialization (restlet jse 2.1rc2) – kky Jan 16 '12 at 16:13
  • actually i'm returning directly an ArrayList instead of a wrapper class with a ArrayList inside – kky Jan 16 '12 at 16:29
  • i've tried also to "Sub-class List" from the jackson doc but it seems i'm doing something wrong, a solution is to wrap the ArrayList in a class, if you return to Restlet that instance all the elements will be wrapped in with the name of the ArrayList instance. In this way the client can perform KVC on the json – kky Jan 16 '12 at 18:46

2 Answers2

1

the solution for me is to annotate the Profile class with:

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.WRAPPER_OBJECT)
public class Profile extends Element implements Serializable {

and now the json now looks like:

{"Profile":{ ... }}

and the return type is a Sub-classed list:

public class ProfileList extends ArrayList<Profile>
{}

see http://wiki.fasterxml.com/JacksonPolymorphicDeserialization 5.1

kky
  • 141
  • 2
  • 13
0

I think what you want is not really available in a sense that JAX-B seems to have some rules on how to deal with lists. See this converstation on the RESTeasy mailing list

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
  • thanks for your reply, i didn't try to return a GenericEntity> x = new GenericEntity>(); the solution of wrapping the ArrayList in a class and returning that instance works fine for me, i was wondering if i could avoid this with a more elegant way.. but it doesn't really matters :) – kky Jan 17 '12 at 14:14