title: Moxy: List of objects XML and JSON can't get both look good
body: <p>I have a problem getting Moxy to generate "good" XML and JSON for a list of inherited objects. Either of the XML looks find or the JSON looks fine, but not at the same time. Here is the model:</p>
  
  <pre><code>public static abstract class Animal {
      private String name;
  
      protected Animal() {
      }
      protected Animal(String name) {
          this.name = name;
      }
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
  }
  @XmlRootElement
  public static class Tiger extends Animal {
      public Tiger() {
      }
      public Tiger(String name) {
          super(name);
      }
  }
  
  @XmlRootElement
  public static class Lion extends Animal {
      public Lion() {
      }
      public Lion(String name) {
          super(name);
      }
  }
  
  @XmlRootElement
  public static class Zoo {
      private List&lt;Animal&gt; animals = new ArrayList&lt;&gt;();
  
      @XmlElementRef
      public List&lt;Animal&gt; getAnimals() {
          return animals;
      }
      public void setAnimals(List&lt;Animal&gt; animals) {
          this.animals = animals;
      }
      public void addAnimal(Animal a) {
          animals.add(a);
      }
  }
  </code></pre>
  
  <p>The XML and JSON generated from the following Zoo instance</p>
  
  <pre><code>    zoo = new Zoo();
      zoo.addAnimal(new Tiger("Hobbes"));
      zoo.addAnimal(new Lion("Simba"));
      zoo.addAnimal(new Tiger("Sherikan"));
  </code></pre>
  
  <p>Look like:</p>
  
  <pre><code>  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
    &lt;zoo&gt;
       &lt;tiger&gt;
          &lt;name&gt;Hobbes&lt;/name&gt;
       &lt;/tiger&gt;
       &lt;lion&gt;
          &lt;name&gt;Simba&lt;/name&gt;
       &lt;/lion&gt;
       &lt;tiger&gt;
          &lt;name&gt;Sherikan&lt;/name&gt;
       &lt;/tiger&gt;
    &lt;/zoo&gt;
    {
       "zoo" : {
          "tiger" : [ {
             "name" : "Hobbes"
          }, {
             "name" : "Sherikan"
          } ],
          "lion" : [ {
             "name" : "Simba"
          } ]
       }
    }
  </code></pre>
  
  <p>The XML looks exactly what I want it too, but the JSON is weird. The way that the Moxy uses the type as key does not preserve order among the elements in the list. Is this a bug or feature?</p>
  
  <p>If I change the annotation on <code>Zoo.animals</code> to <code>@XmlElement</code> the JSON output looks fine, but the XML output has changed to something that I can't use.</p>
  
  <pre><code>  {
       "zoo" : {
          "animals" : [ {
             "type" : "tiger",
             "name" : "Hobbes"
          }, {
             "type" : "lion",
             "name" : "Simba"
          }, {
             "type" : "tiger",
             "name" : "Sherikan"
          } ]
       }
    }
    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
    &lt;zoo&gt;
       &lt;animals xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="tiger"&gt;
          &lt;name&gt;Hobbes&lt;/name&gt;
       &lt;/animals&gt;
       &lt;animals xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="lion"&gt;
          &lt;name&gt;Simba&lt;/name&gt;
       &lt;/animals&gt;
       &lt;animals xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="tiger"&gt;
          &lt;name&gt;Sherikan&lt;/name&gt;
       &lt;/animals&gt;
    &lt;/zoo&gt;
  </code></pre>
  
  <p>The only way I've come up with to solve this is to create an external OXM binding file to override the annotation on <code>Zoo.animals</code>. The binding below is registered using the property of JAXBContextProperties.OXM_METADATA_SOURCE.</p>
  
  <pre><code>  &lt;?xml version="1.0"?&gt;
    &lt;xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema"
                  xml-accessor-type="PUBLIC_MEMBER" xml-accessor-order="ALPHABETICAL"
                  xml-mapping-metadata-complete="false"
                  package-name="com.example"&gt;
       &lt;java-types&gt;
          &lt;java-type name="com.example.JAXBTest2$Zoo"&gt;
             &lt;java-attributes&gt;
                &lt;xml-element java-attribute="animals"/&gt;
             &lt;/java-attributes&gt;
          &lt;/java-type&gt;
       &lt;/java-types&gt;
    &lt;/xml-bindings&gt;
  </code></pre>
  
  <p>Is there a better solution to this that doesn't use an external binding file?</p>