title: JAXB for lists to be returned naturally for JSON or XML
body: <p>I'm using MOXy with Jersey to implement a RESTful API and want to return lists naturally for JSON and XML, by which I mean that the XML contains an element tag for the overall collection as well as the collection items, whereas the JSON contains a tag for the collection only.</p>
  
  <p>For example, I want to return an "organisation" resource with nested lists of facilities and locations.  As XML:</p>
  
  <pre><code>&lt;organisation&gt;
    &lt;id&gt;1&lt;/id&gt;
    &lt;name&gt;XYZ&lt;/name&gt;
    &lt;facilities&gt;
      &lt;facility&gt;
        &lt;id&gt;1&lt;/id&gt;
        &lt;text&gt;Telephone&lt;/text&gt;
      &lt;/facility&gt;
      &lt;facility&gt;
        &lt;id&gt;3&lt;/id&gt;
        &lt;text&gt;Whiteboard&lt;/text&gt;
      &lt;/facility&gt;
    &lt;/facilities&gt;
    &lt;locations&gt;
      &lt;location&gt;
        &lt;id&gt;1&lt;/id&gt;
        &lt;kind&gt;REGION&lt;/kind&gt;
        &lt;name&gt;London&lt;/name&gt;
      &lt;/location&gt;
      &lt;location&gt;
        &lt;id&gt;2&lt;/id&gt;
        &lt;kind&gt;REGION&lt;/kind&gt;
        &lt;name&gt;Manchester&lt;/name&gt;
      &lt;/location&gt;
    &lt;/locations&gt;
  &lt;/organisation&gt;
  </code></pre>
  
  <p>And as JSON:</p>
  
  <pre><code>{
    "id": 1,
    "name": "XYZ",
    "facilities": [
      {
        "id": 1,
        "text": "Telephone"
      },
      {
        "id": 3,
        "text": "Whiteboard"
      }
    ],
    "locations": [
      {
        "id": 1,
        "kind": "REGION",
        "name": "London"
      },
      {
        "id": 2,
        "kind": "REGION",
        "name": "Manchester"
      }
    ]
  }
  </code></pre>
  
  <p>Unfortunately, I don't seem to be able to get a code base that allows me to return the output like this for both XML and JSON. If I use a class to wrap the nested list, then the XML appears correct but not the JSON (see "ExternalFacilities" below). If I define the nested list as an <code>ArrayList</code> sub-class then the JSON appears correct but not the XML (see "ExternalLocations" below).</p>
  
  <p><strong>XML sample showing "facilities" list correct but not "locations"</strong></p>
  
  <p>See that there is no XML element that wraps the "locations" list (as there is for "facilities"), and also that each location has a pluralised element name.</p>
  
  <pre><code>&lt;organisation&gt;
    &lt;id&gt;1&lt;/id&gt;
    &lt;name&gt;XYZ&lt;/name&gt;
    &lt;facilities&gt;
      &lt;facility&gt;
        &lt;id&gt;1&lt;/id&gt;
        &lt;text&gt;Telephone&lt;/text&gt;
      &lt;/facility&gt;
      &lt;facility&gt;
          &lt;id&gt;3&lt;/id&gt;
          &lt;text&gt;Whiteboard&lt;/text&gt;
      &lt;/facility&gt;
    &lt;/facilities&gt;
    &lt;locations&gt;
      &lt;id&gt;1&lt;/id&gt;
      &lt;kind&gt;REGION&lt;/kind&gt;
      &lt;name&gt;London&lt;/name&gt;
    &lt;/locations&gt;
    &lt;locations&gt;
      &lt;id&gt;2&lt;/id&gt;
      &lt;kind&gt;REGION&lt;/kind&gt;
      &lt;name&gt;Manchester&lt;/name&gt;
    &lt;/locations&gt;
  &lt;/organisation&gt;
  </code></pre>
  
  <p><strong>JSON sample showing "locations" list correct but not "facilities"</strong></p>
  
  <p>See that the "facilities" list is a JSON object containing a JSON array, whereas I just want the JSON array (with the pluralised element name).</p>
  
  <pre><code>{
    "id": 1,
    "name": "XYZ",
    "facilities": {
      "facility": [
          {
              "id": 1,
              "text": "Telephone"
          },
          {
              "id": 3,
              "text": "Whiteboard"
          }
      ]
    },
    "locations": [
      {
        "id": 1,
        "kind": "REGION",
        "name": "London"
      },
      {
        "id": 2,
        "kind": "REGION",
        "name": "Manchester"
      }
    ]
  }
  </code></pre>
  
  <p>This samples above are produced using the same code, just changing the Accept HTTP header to have Jersey return JSON instead of XML. Here is an excerpt of the classes:</p>
  
  <p><strong>ExternalOrganisation.java</strong></p>
  
  <pre><code>@XmlRootElement(name="organisation")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class ExternalOrganisation {
  
     private String             name;
     private ExternalFacilities facilities;
     private ExternalLocations  locations;
  
     ...
  
  }
  </code></pre>
  
  <p><strong>ExternalFacilities.java</strong></p>
  
  <pre><code>@XmlRootElement(name="facilities")
  public class ExternalFacilities {
    @XmlElementRef
    private List&lt;ExternalFacility&gt; list;
    ...
  }
  </code></pre>
  
  <p><strong>ExternalLocations.java</strong></p>
  
  <pre><code>@XmlRootElement(name="locations")
  public class ExternalLocations extends ArrayList&lt;ExternalLocation&gt; {
    ...
  }
  </code></pre>
  
  <p><strong>ExternalFacility.java</strong></p>
  
  <pre><code>@XmlRootElement(name="facility")
  @XmlType(propOrder={"id", "uri", "kind", "text"})
  public class ExternalFacility extends ExternalBase {
      // id inherited
      private String text;
      ....
  }
  </code></pre>
  
  <p><strong>ExternalLocation.java</strong></p>
  
  <pre><code>@XmlRootElement(name="location")
  @XmlType(propOrder={"id", "kind", "name"})
  public class ExternalLocation extends ExternalBase {
      // id inherited
      @XmlElement private LocationKind kind;
      @XmlElement private String       name;
      ...
  }
  </code></pre>
  
  <p>Seems at first similar to <a href="https://stackoverflow.com/questions/20569891/moxy-list-of-objects-xml-and-json-cant-get-both-look-good">this question</a> but I'm not trying to mix the types of object in my lists.</p>