1

I have an XML schema for UI controls that looks something like the following XML

<InputParameters>
    <Combobox name="Combo1">...</Combobox>
    <Radiobutton name="Radio1">...</Radiobutton>
    <Combobox name="Combo2">...</Combobox>
    <Combobox name="Combo2">...</Combobox>
</InputParameters>

Here is how InputParameters (a field within another JAXB element) is declared:

    @XmlElementWrapper(name="InputParameters")
    @XmlElementRefs(
        {
            @XmlElementRef(name="Combobox",     type=ComboboxJAXB.class),
            @XmlElementRef(name="RadioButtonGroup", type=RadioButtonGroup.class)
        }
        )
    protected List<? extends InputComponent> inputParameters;

A few important points here:

  • The order of items in InputParameters is important
  • Combobox and Radiobutton all have the same substitution group head in the schema (InputComponent)

Might also be worth observing that:

  • InputParameters can (and is) thought of as Map where the @name attributes are (unique) keys
  • Since order is important, this would have to be a Java LinkedHashMap

The XML output from marshaling (shown above) is fine. Where I'm having trouble is when JAX-RS outputs this as JSON.

In JSON, the output looks something like:

{
    InputParameters : {
       Combobox : {.....},
       Radiobutton : {.......},
       Combobox : {.....},
       Combobox : {.....},
       }
}

The problems here are:

  • the InputParameters object has multiple properties named "Combobox"
  • because InputParameters is an object rather than an array the order of items is not represented

The question boils down to this: Can I get JAXB to output the content of InputParameters as an (ordered) array rather than an (unordered) set of objects? Or perhaps there's just some very different approach I should consider?

The desired JSON output looks more like:

{
    InputParameters : 
      [
         {Combobox : {.....}},
         {Radiobutton : {.......}},
         {Combobox : {.....}},
         {Combobox : {.....}},
      ]
}

I am using the reference implementation of JAXB.

DaBlick
  • 898
  • 9
  • 21

0 Answers0