Hi Guillaume,
I think the easiest approach in this respect is to switch from using
Jersey's JAXB/JSON writers to using Jackson's JAX-RS writers that
support POJOs (you do not need to use the low-level Jackson support).
That way it should be possible to use JAXB for XML serialization and
Jackson for JSON serialization.
Paul.
On May 26, 2010, at 6:51 PM, Zurbach Guillaume wrote:
> Hi,
>
> I meet a problem that has already been mentioned twice on jersey-
> users list :
> - http://markmail.org/thread/xhebc2jx5fnaq2ny
> - http://markmail.org/thread/enalt6uxfydkxytb
>
> I have a resource that produces both Json and XML output from two
> POJO class : User and Group.
> Groups contains users and vice versa.
>
> @XmlRootElement
> public class User {
>
> protected String login;
> protected String password;
> protected String email;
>
> […]
>
> @XmlTransient
> public List<Group> getGroups() {
> return groups;
> }
> }
>
> @XmlRootElement
> public class Group {
>
> protected String name;
> protected List<User> users;
>
> […]
>
> @XmlElementWrapper(name = "users")
> @XmlElement(name = "user")
> public List<User> getUsers() {
> return users;
> }
> }
>
> I use @XmlElementWrapper annotation in order to obtain well-
> structured XML (in Natural notation), like this :
>
> <group>
> <name>GroupName</name>
> <users>
> <user>
> <login>User1Login</login>
> </user>
> <user>
> <login>User2Login</login>
> </user>
> </users>
> </group>
>
> But, in this case, Json output is not correct and I have problems to
> manipulate it.
>
> {"name":"GroupName","users":[{"user":{"login":"User1Login"},"user":
> {"login":"User2Login"}}]}
>
> So, if I comment @XmlElementWrapper and @XmlElement annotations, I
> obtain a good Json output, like this…
>
> {"name":"GroupName","users":[{"login":"User1Login"},
> {"login":"User2Login"}]}
>
> But my XML output is not correct :
>
> <group>
> <name>GroupName</name>
> <users>
> <login>User1Login</login>
> </users>
> <users>
> <login>User2Login</login>
> </users>
> </group>
>
> I try to check out and study the source code of jersey-json to see
> if it may possible to create a new JSONConfiguration notation. It
> appears that each notation use a different stream writer, which are
> quite complicated to manipulate.
> I am currently working on RESTHub project (http://resthub.org) and
> we try to do generic code, that’s why this isn’t possible for us to
> use low-level JSON support with Jackson.
>
> So, here are my questions :
> - the resemblance between XML and Json outputs suggests
> that the Json marshalling is made from the XML, is it true ?
> - what is the easiest way to get both well-formed XML and
> Json output ?
> - is it possible to ignore @XmlElementWrapper and
> @XmlElement on Json marshalling ?
>
> This problem is a central for our project and we are ready to
> contribute to jersey development if necessary, we just need
> information on the best way to improve Json serialization in Jersey.
>
> Thank you in advance for any information ;)
> Guillaume
>
>
> Ce message et les pièces jointes sont confidentiels et réservés à
> l'usage exclusif de ses destinataires. Il peut également être
> protégé par le secret professionnel. Si vous recevez ce message par
> erreur, merci d'en avertir immédiatement l'expéditeur et de le
> détruire. L'intégrité du message ne pouvant être assurée sur
> Internet, la responsabilité du groupe Atos Origin ne pourra être
> recherchée quant au contenu de ce message. Bien que les meilleurs
> efforts soient faits pour maintenir cette transmission exempte de
> tout virus, l'expéditeur ne donne aucune garantie à cet égard et sa
> responsabilité ne saurait être recherchée pour tout dommage
> résultant d'un virus transmis.
>
> This e-mail and the documents attached are confidential and intended
> solely for the addressee; it may also be privileged. If you receive
> this e-mail in error, please notify the sender immediately and
> destroy it. As its integrity cannot be secured on the Internet, the
> Atos Origin group liability cannot be triggered for the message
> content. Although the sender endeavours to maintain a computer virus-
> free network, the sender does not warrant that this transmission is
> virus-free and will not be liable for any damages resulting from any
> virus transmitted.