users@glassfish.java.net

Jersey + JSON + JAXB + Natural

From: <glassfish_at_javadesktop.org>
Date: Tue, 17 Nov 2009 15:43:12 PST

Hi Java Community--
        
        I have a problem with Jersey & JSON Notation...

        
        I have a working Jersey prototype project that uses the default "mapping" notation.

        I have about 8 tests that do the ususal POST, GET, PUT, and DELETE-- works great! (thank you jersey team for this great framework)


        There is however one gripe: the JSON Notation

        Let me give you some background about how I do things:


        1) I generate my jaxb objects using an XSD file-- so i declare my XSD file.

        2) I use of "XML Attributes" in my XSD file-- this cuts down on the XML payload size--

                for ex:
                
                this:
                        <config pollingInterval="100" />

                is ALOT smaller than:
                        
                        <config><pollingInterval>100</pollingInterval></config>

                so I generally see XML attributes as more effecient (my perspective)


        Given #1 and #2 the default JSON "mapping" notation produces JSON that is littered with "@".

        Right now the "@" is the bane of my existence-- cause the javascript world does not like it at all.

         
        So there are two straitfoward solutions-- either i have the client deal with the "@" or I have the server not send the "@"
        

        Client Side Solution:

                find and replace the "@ with an empty sting ('')-- yuck, this does not feel clean.

        Server Side Solution:

                Use a "JSONConfiguration" with NATURAL notation-- I have this halfway working.

                The NATURAL notation is not (i am guessing) able to bind back into the JAXB Object.

                The reason I say this is because:

                        I have a working Jersey Client that uses JAXB objects fine with the mapping notation, but fails with the natrual notation.

                        There is no "exception being thrown" its just that when I use the client to "GET" a jaxb object from the RESTful server,
                        
                        it reterns an empty JAXB object with all hte members null and not set.
                        

        My prototype project is "maven-ized" and I am willing to post the entire project or code snippets as requested... for not I will post my JSONConfiguration


        So in a nutshell:


@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {

        private JAXBContext context;
        private Class[] types = { Coupon.class, Coupons.class, Comment.class, Comments.class };

        public JAXBContextResolver() throws Exception {
                //this.context = new JSONJAXBContext(JSONConfiguration.mapped().build(), types); (THIS WORKS)
                this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), types); // THIS DOES NOT WORK)
        }

        public JAXBContext getContext(Class<?> objectType) {
                for (Class type : types) {
                        if (type == objectType) {
                                return context;
                        }
                }
                return null;
        }

}


        Again, there is no exception being thrown, the issue is: If I use the Jersey Client to "GET" a JAXB object (that i know exists) -- it returns a JAXB object that has none of the members set/injected.

        Please keep in mind that my JAXB Object members are annotated with "XMLAttribute" because I generate them from an XSD where I use alot of XML Attributes.
[Message sent by forum member 'hokusmokus' ]

http://forums.java.net/jive/thread.jspa?messageID=372262