users@jersey.java.net

[Jersey] Jersey, mapped JSON notation, JsonXmlStreamReader and object inheritance

From: Kytömäki, Janne <janne.kytomaki_at_logica.com>
Date: Mon, 22 Sep 2008 10:30:52 +0200

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