Jersey JSON, Inheritance, jsrender

225 Views Asked by At

I am currently working on a solution that let me control my Linn devices via RS232. I do have modeled each device as a separate class e.g. LinnKINOS, inheriting von a general class e.g. RS232Device, as the devices do have different information to query. A Kinos, for example does habe a property Surround, a 'normal' amp does not.

@Xml RootElement  ( name = "RS232Device")
@JsonRootName(value = "RS232Device")
public class RS232Device implements Comparable
{
    String name;
    String identifier;
    String groupid;
    String versionSW;
    String versionHW;
    String bas;
    String treb;
    String volume;
    String listen;
    String balance;
    String mute;
    String path;
    String origin;
    boolean isDaisyChain;
    HashMap<String,String> content = new HashMap<String, String>();
    String power;
    String mains;

...
}


@XmlRootElement  (name = "LinnKINOS")
@JsonRootName(value = "LinnKINOS")
public class LinnKINOS extends RS232Device
{
    String surround;
 .....
}

The instances of the classes are saved within a HashMap HashMap<String,RS232Device>. A Webclient will then display the status of the devices and one can issue commands to each of the devices. The rending of the data is done by JSRender.

The method that is called from the frontend is the following:

@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public List<RS232Device> findIDs() {
    log.info("getIDs");
    return dao.getIDs();
}

When marshaling the array, the instances of RS232Device and LinnKinos are always marshaled as RS232Device. Unfortunately the extra information of the LinnKINOS class (here surround) is not marshaled.

Object
RS232Device: Array[2]
0: Object
content: Object
daisyChain: "true"
identifier: "KINOS"
ir: "ON"
mute: "OFF"
name: "UNKNOWN"
path: "@KINOS@"
status: "0"
volume: "40"
__proto__: Object
1: Object
content: Object
daisyChain: "false"
identifier: "UNKNOWN_DEVICE"
name: "UNKNOWN"
path: ""
status: "0"
__proto__: Object
length: 2
__proto__: Array[0]
__proto__: Object

One solution to ensure that the extra Information in the LinnKinos class is marshaled, is to enable com.sun.jersey.api.json.POJOMappingFeature in the web.xml

<init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
</init-param>

When doing so, the Objects are marshaled as an array of objects like this:

[
Object
balance: null
bas: null
content: Object
daisyChain: true
description: null
groupid: null
identifier: "KINOS"
image: null
ir: "ON"
link: null
listen: null
mains: null
mute: "OFF"
name: "UNKNOWN"
origin: null
path: "@KINOS@"
power: null
status: 0
surround: null
treb: null
versionHW: null
versionSW: null
volume: "40"
__proto__: Object
, 
Object
balance: null
bas: null
content: Object
daisyChain: false
description: null
groupid: null
identifier: "UNKNOWN_DEVICE"
image: null
link: null
listen: null
mains: null
mute: "OFF"
name: "UNKNOWN"
origin: null
path: ""
power: null
status: 0
treb: null
versionHW: null
versionSW: null
volume: null
__proto__: Object

With that, my JSRender functionality for that part does not work any longer.

<script id="DevicePanelTemplate" type="text/x-jsrender">
    {{for RS232Device}}
        {{if identifier !== ""}}
            <li><a href="pageDeviceCommands?id={{>identifier}}" class="icon-{{>identifier}}">{{>identifier}}</a></li>
        {{/if}}
    {{/for}}
</script>

I have tried to change the JSRender functionality to work with the plain objects, but without success.

I am also not keen just to add all the extra properties to the Baseclass, and use the different identifiers to check what kind a device is....

Is there a way to either tell Jersey to marshall the extra properties of the inherited class and still use the BaseClass as identifier for the array (without coding my own Marhaller), or to tell JSRender to just work on the objects ( for member did not work for me).

The versions I have used are: JDK 1.7 JBoss 7.1.1 Jersey 1.1.8 JSRender v1.0.0-beta

Many thanks in advance, Eduard

0

There are 0 best solutions below