1
votes

I am trying to serialize a java data-structure to JSON using JAXB/MOXy. The datastructure contains a list of elements of different type (with a common base-class). In this case, the serializer groups the elements by their concrete type, but i would like the items to be in their original order and include the subtype as a value of the corresponding entry.

Below is a little test-instance that shows the problem. A ElementGroup can contain child-elements that are of the type ElementString, ElementNumber or ElementGroup.

Element:

@XmlRootElement(name = "Element")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({ElementGroup.class, ElementString.class, ElementNumber.class})
public abstract class Element
{
    @XmlAttribute(name = "name")
    public final String name;

    public Element()
    {
        this.name = null;
    }
    public Element(String name)
    {
        this.name = name;
    }
}

ElementGroup:

@XmlRootElement(name = "ElementGroup")
@XmlAccessorType(XmlAccessType.FIELD)
public class ElementGroup extends Element
{
    @XmlElementRef()
    public final List<Element> elements;

    public ElementGroup()
    {
        this.elements = new ArrayList<>();
    }
    public ElementGroup(String name, List<Element> elements)
    {
        super(name);
        this.elements = elements;
    }
}

ElementString:

@XmlRootElement(name = "ElementString")
@XmlAccessorType(XmlAccessType.FIELD)
public class ElementString extends Element
{
    @XmlAttribute(name = "value")
    public final String value;

    public ElementString()
    {
        this.value = null;
    }
    public ElementString(String name, String value)
    {
        super(name);
        this.value = value;
    }
}

ElementNumber:

@XmlRootElement(name = "ElementNumber")
@XmlAccessorType(XmlAccessType.FIELD)
public class ElementNumber extends Element
{
    @XmlAttribute(name = "value")
    public final Integer value;

    public ElementNumber()
    {
        this.value = null;
    }
    public ElementNumber(String name, int value)
    {
        super(name);

        this.value = value;
    }
}

When I try to serialize the following instance, i get the result below:

 ElementNumber number1 = new ElementNumber("Num1", 1);
 ElementString string1 = new ElementString("String1", "one");
 ElementNumber number2 = new ElementNumber("Num2", 2);
 ElementString string2 = new ElementString("String2", "two");

 Element result = new ElementGroup("root", Arrays.asList(number1, string1, number2, string2));

The serializer groups the child-elements of ElementGroup by their type (first an array with the name "ElementNumber", then an array with the name "ElementString").

{
    "type": "elementGroup",
    "name": "root",
    "ElementNumber": [
        {
            "name": "Num1",
            "value": 1
        },
        {
            "name": "Num2",
            "value": 2
        }
    ],
    "ElementString": [
        {
            "name": "String1",
            "value": "one"
        },
        {
            "name": "String2",
            "value": "two"
        }
    ]
}

I would prefer something like this however, where the original order of the elements is maintained, and the type is included as a value instead of as the array-name:

{
    "type": "elementGroup",
    "name": "root",
    "elements": [
        {
            "type": "elementNumber",
            "name": "Num1",
            "value": 1
        },
        {
            "type": "elementString",
            "name": "String1",
            "value": "one"
        },
        {
            "type": "elementNumber",
            "name": "Num2",
            "value": 2
        },
        {
            "type": "elementString",
            "name": "String2",
            "value": "two"
        }
    ]
}

Is there some JAXB or a MOXy-specific annotation that i can use to get MOXy to produce something like this?

The code is used in a Jersey-Webservice, so I don't think i can change the serializer-instance, therefore something that can be done purely with annotations would be best.

Also, i would prefer to use MOXy instead of another JAXB implementation like Jackson, because XML serialization is also required and already works as intended with MOXy.

1

1 Answers

1
votes

In ElementGroup change @XmlElementRef to @XmlElement(name="elements") like below and you will get what you want in json

//@XmlElementRef
    @XmlElement(name="elements") 
    public final List<Element> elements;