title: How do I output null elements and attributes when outputting JSON using EclipseLink MOXy
body: <p>In my XML I only output an element if it has a value, but the requirement for the JSON is to output the value even if not set</p>
  
  <p>i.e
  Xml</p>
  
  <pre><code>&lt;alias-list&gt;
  &lt;alias sort-name="Afghan"&gt;Afghany&lt;/alias&gt;
  &lt;/alias-list&gt;
  </code></pre>
  
  <p>I would like JSON to output other elements of the alias element even though not set</p>
  
  <p>i.e</p>
  
  <pre><code>   "aliases" : [ {
        sort-name : "Afghan"
        begin-date : null
        end-date : null
        value : "Afghany"
     } ]
  </code></pre>
  
  <p>but all I can do is have a method that I use when ouputting json to set these null elements to empty string, this gives me</p>
  
  <pre><code>   "aliases" : [ {
        sort-name : "Afghan"
        begin-date : ""
        end-date : ""
        value : "Afghany"
     } ]
  </code></pre>
  
  <p>but that is not what I want</p>
  
  <p><strong>Update with attempt to use Denises answer</strong></p>
  
  <p>I've ran into a few problems with the proposed solution, first here is the full Alias class I think that would help (note the classes autogenned with JAXB, I came across this before ElipseLink and because my Xml output is okay Im not particulary keen to change this)</p>
  
  <pre><code>//
  // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-792 
  // See &lt;a href="http://java.sun.com/xml/jaxb"&gt;http://java.sun.com/xml/jaxb&lt;/a&gt; 
  // Any modifications to this file will be lost upon recompilation of the source schema. 
  // Generated on: 2013.06.04 at 02:00:21 PM BST 
  //
  
  
  package org.musicbrainz.mmd2;
  
  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  import javax.xml.bind.annotation.XmlAttribute;
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.bind.annotation.XmlSchemaType;
  import javax.xml.bind.annotation.XmlType;
  import javax.xml.bind.annotation.XmlValue;
  
  
  /**
   * &lt;p&gt;Java class for anonymous complex type.
   * 
   * &lt;p&gt;The following schema fragment specifies the expected content contained within this class.
   * 
   * &lt;pre&gt;
   * &amp;lt;complexType&gt;
   *   &amp;lt;complexContent&gt;
   *     &amp;lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
   *       &amp;lt;attribute name="locale" type="{http://musicbrainz.org/ns/mmd-2.0#}def_iso-3166-2-code" /&gt;
   *       &amp;lt;attribute name="sort-name" type="{http://www.w3.org/2001/XMLSchema}anySimpleType" /&gt;
   *       &amp;lt;attribute name="type" type="{http://www.w3.org/2001/XMLSchema}anySimpleType" /&gt;
   *       &amp;lt;attribute name="primary" type="{http://www.w3.org/2001/XMLSchema}anySimpleType" /&gt;
   *       &amp;lt;attribute name="begin-date" type="{http://musicbrainz.org/ns/mmd-2.0#}def_incomplete-date" /&gt;
   *       &amp;lt;attribute name="end-date" type="{http://musicbrainz.org/ns/mmd-2.0#}def_incomplete-date" /&gt;
   *     &amp;lt;/restriction&gt;
   *   &amp;lt;/complexContent&gt;
   * &amp;lt;/complexType&gt;
   * &lt;/pre&gt;
   * 
   * 
   */
  @XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "", propOrder = {
      "content"
  })
  @XmlRootElement(name = "alias")
  public class Alias {
  
      @XmlValue
      protected String content;
      @XmlAttribute
      protected String locale;
      @XmlAttribute(name = "sort-name")
      @XmlSchemaType(name = "anySimpleType")
      protected String sortName;
      @XmlAttribute
      @XmlSchemaType(name = "anySimpleType")
      protected String type;
      @XmlAttribute
      @XmlSchemaType(name = "anySimpleType")
      protected String primary;
      @XmlAttribute(name = "begin-date")
      protected String beginDate;
      @XmlAttribute(name = "end-date")
      protected String endDate;
  
      /**
       * Gets the value of the content property.
       * 
       * @return
       *     possible object is
       *     {@link String }
       *     
       */
      public String getContent() {
          return content;
      }
  
      /**
       * Sets the value of the content property.
       * 
       * @param value
       *     allowed object is
       *     {@link String }
       *     
       */
      public void setContent(String value) {
          this.content = value;
      }
  
      /**
       * Gets the value of the locale property.
       * 
       * @return
       *     possible object is
       *     {@link String }
       *     
       */
      public String getLocale() {
          return locale;
      }
  
      /**
       * Sets the value of the locale property.
       * 
       * @param value
       *     allowed object is
       *     {@link String }
       *     
       */
      public void setLocale(String value) {
          this.locale = value;
      }
  
      /**
       * Gets the value of the sortName property.
       * 
       * @return
       *     possible object is
       *     {@link String }
       *     
       */
      public String getSortName() {
          return sortName;
      }
  
      /**
       * Sets the value of the sortName property.
       * 
       * @param value
       *     allowed object is
       *     {@link String }
       *     
       */
      public void setSortName(String value) {
          this.sortName = value;
      }
  
      /**
       * Gets the value of the type property.
       * 
       * @return
       *     possible object is
       *     {@link String }
       *     
       */
      public String getType() {
          return type;
      }
  
      /**
       * Sets the value of the type property.
       * 
       * @param value
       *     allowed object is
       *     {@link String }
       *     
       */
      public void setType(String value) {
          this.type = value;
      }
  
      /**
       * Gets the value of the primary property.
       * 
       * @return
       *     possible object is
       *     {@link String }
       *     
       */
      public String getPrimary() {
          return primary;
      }
  
      /**
       * Sets the value of the primary property.
       * 
       * @param value
       *     allowed object is
       *     {@link String }
       *     
       */
      public void setPrimary(String value) {
          this.primary = value;
      }
  
      /**
       * Gets the value of the beginDate property.
       * 
       * @return
       *     possible object is
       *     {@link String }
       *     
       */
      public String getBeginDate() {
          return beginDate;
      }
  
      /**
       * Sets the value of the beginDate property.
       * 
       * @param value
       *     allowed object is
       *     {@link String }
       *     
       */
      public void setBeginDate(String value) {
          this.beginDate = value;
      }
  
      /**
       * Gets the value of the endDate property.
       * 
       * @return
       *     possible object is
       *     {@link String }
       *     
       */
      public String getEndDate() {
          return endDate;
      }
  
      /**
       * Sets the value of the endDate property.
       * 
       * @param value
       *     allowed object is
       *     {@link String }
       *     
       */
      public void setEndDate(String value) {
          this.endDate = value;
      }
  
  }
  </code></pre>
  
  <p>When I try the json binding solution I get </p>
  
  <pre><code>Exception Description: The property or field beginDate on the class org.musicbrainz.mmd2.Alias is required to be included in the propOrder element of the XmlType annotation.
   - with linked exception:
  [Exception [EclipseLink-50013] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.JAXBException
  Exception Description: The property or field beginDate on the class org.musicbrainz.mmd2.Alias is required to be included in the propOrder element of the XmlType annotation.]
      at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1021)
      at org.eclipse.persistence.jaxb.JAXBContext.&lt;init&gt;(JAXBContext.java:174)
      at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:165)
      at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:152)
      at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:112)
      at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:102)
      at org.musicbrainz.search.servlet.mmd2.ResultsWriter.initJsonContext(ResultsWriter.java:100)
      ... 25 more
  </code></pre>
  
  <p>So i then edited Alias.java to add beginDate to propOrder, but this gave this error. Which is true - content is annotated with @XmlValue but I dont understand why that is a problem. </p>
  
  <pre><code>Exception Description: The property or field beginDate must be an attribute because another field or property is annotated with XmlValue.]
      at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1021)
      at org.eclipse.persistence.jaxb.JAXBContext.&lt;init&gt;(JAXBContext.java:174)
      at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:165)
      at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:152)
      at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:112)
      at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:102)
      at org.musicbrainz.search.servlet.mmd2.ResultsWriter.initJsonContext(ResultsWriter.java:100)
      ... 30 more
  </code></pre>
  
  <p>Unfortunately I cannot use the @XmlElement option because beginDate is an attribute not an element and already has a @XmlAttribute annotation, and this does not seem to support nillable. </p>