users@jersey.java.net

Re: [Jersey] Jersey, mapped JSON notation, JsonXmlStreamReader and object inheri

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Mon, 22 Sep 2008 17:56:20 +0200

Hi Janne,

On Mon, Sep 22, 2008 at 05:01:07PM +0200, Kytömäki, Janne wrote:
> Hi Jakub,
>
> Thank you for the quick reply!
>
> I've posted this feature request as issue 113.

Great, thanks!
>
> I'll try your suggestion to use the MAPPED_JETTISON notation. I tried putting the following into the JAXB context resolver:
>
> ..
> props.put(JSONJAXBContext.JSON_NOTATION, JSONJAXBContext.JSONNotation.MAPPED_JETTISON.name());
> props.put(JSONJAXBContext.JSON_XML2JSON_NS, "{\"http://www.w3.org/2001/XMLSchema-instance\":\"xsi\"}");
> ..
> this.context = new JSONJAXBContext(types, props);
> ..
>
> (I'm currently using Jersey 0.9-ea), but I'm getting an InstantiationException. Am I setting the JSON_XML2JSON_NS property correctly? Sorry if this is documented somewhere.

I think (not testing it), you should put backslashes before the / in the url
(JSONObject is used under the covers to parse the config option):
"{\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema-instance\":\"xsi\"}"

Btw. I have just (in the main trunk) changed the way, how things are configured.
Now it should be possible to use just:

props.put(JSONJAXBContext.JSON_XML2JSON_NS,
          new HashMap<String, String>(1){{
                put("http://www.w3.org/2001/XMLSchema-instance", "xsi");
            }});

>
> Also I've played around with Jettison for JSON serialization for a bit in the beginning of the current project, but had some weird problems with deeply nested properties, which is why I moved to Jersey. So for me it would be easiest to just get inheritance/namespaces for Jersey's mapped JSON notation.. :-)

I see. It is always good to know the motivation. Thanks for sharing it.

~Jakub

>
> -Janne
>
>
> Date: Mon, 22 Sep 2008 14:10:29 +0200
> From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
> Content-type: text/plain; charset=iso-8859-1
> Subject: [Jersey] Jersey, mapped JSON notation, JsonXmlStreamReader and object inheri
>
>
> Hi Janne,
>
> The feature is not supported yet. Could you please file it as a RFE at [1]?
> The primary target for JSON internal notation was to provide a very simple JSON format.
> Namespace support was something i thought we could put aside for some time.
> Maybe there is time to think about it again.
>
> Have you tried if specifying other JSON notations help?
> Namely, you can use [JSONJAXBContext.JSONNotation.MAPPED_JETTISON] notation
> and pass it a map for namespaces conversion as a [JSONJAXBContext.JSON_XML2JSON_NS]
> option (via custom JAXB context resolver).
>
> Would it work for you?
>
> ~Jakub
>
>
> [1]https://jersey.dev.java.net/issues/
>
> On Mon, Sep 22, 2008 at 10:30:52AM +0200, Kytömäki, Janne wrote:
> > Hi,
> >
> > I'm having a problem with posting JSON objects (mapped notation) to Jersey that have property classes that use inheritance.
> >
> > For example with the following classes:
> >
> > public class TestObject{
> > private SuperClass superClass;
> > public SuperClass getSuperClass(){
> > return this.superClass;
> > }
> > public void setSuperClass(SuperClass superClass){
> > this.superClass = superClass;
> > }
> > }
> >
> > public class SuperClass{
> > }
> >
> > public class SubClass extends SuperClass{
> > private String name;
> > public String getName(){
> > return this.name;
> > }
> > public void setName(String name){
> > this.name = name;
> > }
> > }
> >
> > The marhshalled XML document I get from Jersey is:
> >
> > <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> > <testObject>
> > <superClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="subClass">
> > <name>Foo</name>
> > </superClass>
> > </testObject>
> >
> > And JSON document:
> >
> > {"testObject":{"superClass":{"@type":"subClass","name":"Foo"}}}
> >
> > Now if I send the XML document back to Jersey, the unmarshalled TestObject instance is fine i.e. it has an instance of SubClass in the superClass property. However if I send the object back as JSON, the @type property gets ignored and the "superClass" property is instantiated as a SuperClass instance instead of SubClass and its "name" property is lost.
> >
> > I've checked the source code for Jersey JSON, and the problem seems to be that namespaces are not supported for JSON, at least for the mapped notation: JsonReaderXmlEvent.getAttributeNamespace(index) always returns null and JsonNamespaceContext.getNamespaceURI(prefix) throws UnsupportedOperationException. Is the problem with the notation, or just that the feature is not yet supported? Any suggestions to get around the problem?
> >
> > Thanks!
> >
> > Regards,
> > Janne
> >
>
> --
> http://blogs.sun.com/japod
>
>
>
>
>
>

-- 
http://blogs.sun.com/japod