title: How do I serialize a JAXBElement?
body: <p>I am using: </p>
  
  <ul>
  <li>GSON 2.3.1</li>
  <li>apache-cxf-3.1.1</li>
  <li>JBoss EAP 6.4</li>
  </ul>
  
  <p>I am using wsdl2java to create webservice client from wsdl.</p>
  
  <p>I generate the classes with: </p>
  
  <pre><code>@XmlElementRef(name = "FMLA_Code", namespace = "http://www.ultipro.com/contracts", type = JAXBElement.class)
  protected JAXBElement&lt;String&gt; fmlaCode;
  </code></pre>
  
  <p>When getting an object back from the webservice call I want to make a JSON response to the caller</p>
  
  <pre><code>EmploymentInformationGetResponse response =             port.getEmploymentInformationByEmployeeIdentifier(employeeNumberIdentifier);
  GsonBuilder gsonBuilder = new GsonBuilder();
  gsonBuilder.registerTypeHierarchyAdapter(EmploymentInformationGetResponse.class, new EmploymentInformationGetResponseAdapter());
  Gson gson = gsonBuilder.disableHtmlEscaping().create();
  String s = gson.toJson(response);
  System.out.println(s);
  </code></pre>
  
  <p>The result contains</p>
  
  <pre><code>fmlaCode=javax.xml.bind.JAXBElement@3191394e
  </code></pre>
  
  <p>What is the best way for me to get the value?</p>
  
  <p>I can but would prefer not the alter the generated classes, as they might need to be generated again later. </p>
  
  <p>I tried to create a type adapter for <code>JAXBElement</code>, but it does not get called:</p>
  
  <pre><code>public class JAXBElementAdapter extends TypeAdapter&lt;JAXBElement&gt; {
  
      @Override
      public void write(JsonWriter jsonWriter, JAXBElement jaxbElement) throws IOException {
          if (jaxbElement == null) {
              jsonWriter.nullValue();
              return;
          }
          JAXBElement e = jaxbElement;
          jsonWriter.value(e.getValue() != null ? e.getValue().toString() : "");
      }
  
      @Override
      public JAXBElement read(JsonReader jsonReader) throws IOException {
          return null;
      }
  }
  </code></pre>
  
  <p>I used this code to register the adapter:</p>
  
  <pre><code>    gsonBuilder.registerTypeHierarchyAdapter(JAXBElement.class, new JAXBElementAdapter());
  </code></pre>
  
  <p>Here is the generated EmploymentInformationGetResponse</p>
  
  <pre><code>import javax.xml.bind.JAXBElement;
  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  import javax.xml.bind.annotation.XmlElementRef;
  import javax.xml.bind.annotation.XmlType;
  import java.util.List;
  
  /**
   * &lt;p&gt;Java class for EmploymentInformationGetResponse complex type.
   * 
   * &lt;p&gt;The following schema fragment specifies the expected content contained within this class.
   * 
   * &lt;pre&gt;
   * &amp;lt;complexType name="EmploymentInformationGetResponse"&amp;gt;
   *   &amp;lt;complexContent&amp;gt;
   *     &amp;lt;extension base="{http://www.ultipro.com/contracts}GetResponse"&amp;gt;
   *       &amp;lt;sequence&amp;gt;
   *         &amp;lt;element name="Results" type="{http://www.ultipro.com/contracts}ArrayOfEmploymentInformation" minOccurs="0"/&amp;gt;
   *       &amp;lt;/sequence&amp;gt;
   *     &amp;lt;/extension&amp;gt;
   *   &amp;lt;/complexContent&amp;gt;
   * &amp;lt;/complexType&amp;gt;
   * &lt;/pre&gt;
   * 
   * 
   */
  @XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "EmploymentInformationGetResponse", propOrder = {
      "results"
  })
  public class EmploymentInformationGetResponse
      extends GetResponse
  {
  
      @XmlElementRef(name = "Results", namespace = "http://www.ultipro.com/contracts", type = JAXBElement.class)
      protected JAXBElement&lt;ArrayOfEmploymentInformation&gt; results;
  
      /**
       * Gets the value of the results property.
       * 
       * @return
       *     possible object is
       *     {@link JAXBElement }{@code &lt;}{@link ArrayOfEmploymentInformation }{@code &gt;}
       *     
       */
      public JAXBElement&lt;ArrayOfEmploymentInformation&gt; getResults() {
          return results;
      }
  
      /**
       * Sets the value of the results property.
       * 
       * @param value
       *     allowed object is
       *     {@link JAXBElement }{@code &lt;}{@link ArrayOfEmploymentInformation }{@code &gt;}
       *     
       */
      public void setResults(JAXBElement&lt;ArrayOfEmploymentInformation&gt; value) {
          this.results = value;
      }
  </code></pre>
  
  <p>And the EmploymentInformation without getters and setters </p>
  
  <pre><code>/**
   * 
   */
  @XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "EmploymentInformation", propOrder = {
      "arrearsSuspendedFrom",
      "arrearsSuspendedTo",
      "beneSeniority",
      "deceased",
      "deceasedDate",
      "earlyRetirement",
      "employeeIdentifier",
      "employmentStatus",
      "fmlaCode",
      "hcsoEndDate",
      "hcsoNotCovered",
      "hcsoStartDate",
      "job",
      "jobStart",
      "lastHire",
      "lastPerfReview",
      "lastSalaryReview",
      "leaveReason",
      "nextPerfReview",
      "nextSalaryReview",
      "originalHire",
      "ptoSuspendedFrom",
      "ptoSuspendedTo",
      "payAutomatically",
      "paySuspendedFrom",
      "paySuspendedTo",
      "roeIssueReason",
      "regularRetirement",
      "selfServiceProperties",
      "seniority",
      "statusAnticipatedEnd",
      "statusStartDate",
      "weeks"
  })
  public class EmploymentInformation {
  
      @XmlElement(name = "ArrearsSuspendedFrom")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar arrearsSuspendedFrom;
      @XmlElement(name = "ArrearsSuspendedTo")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar arrearsSuspendedTo;
      @XmlElement(name = "BeneSeniority")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar beneSeniority;
      @XmlElement(name = "Deceased")
      protected Boolean deceased;
      @XmlElement(name = "DeceasedDate")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar deceasedDate;
      @XmlElement(name = "EarlyRetirement")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar earlyRetirement;
      @XmlElementRef(name = "EmployeeIdentifier", namespace = "http://www.ultipro.com/contracts", type = JAXBElement.class)
      protected JAXBElement&lt;EmployeeIdentifier&gt; employeeIdentifier;
      @XmlElementRef(name = "EmploymentStatus", namespace = "http://www.ultipro.com/contracts", type = JAXBElement.class)
      protected JAXBElement&lt;String&gt; employmentStatus;
      @XmlElementRef(name = "FMLA_Code", namespace = "http://www.ultipro.com/contracts", type = JAXBElement.class)
      protected JAXBElement&lt;String&gt; fmlaCode;
      @XmlElement(name = "HCSOEndDate")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar hcsoEndDate;
      @XmlElement(name = "HCSONotCovered")
      protected Boolean hcsoNotCovered;
      @XmlElement(name = "HCSOStartDate")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar hcsoStartDate;
      @XmlElementRef(name = "Job", namespace = "http://www.ultipro.com/contracts", type = JAXBElement.class)
      protected JAXBElement&lt;String&gt; job;
      @XmlElement(name = "JobStart")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar jobStart;
      @XmlElement(name = "LastHire")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar lastHire;
      @XmlElement(name = "LastPerfReview")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar lastPerfReview;
      @XmlElement(name = "LastSalaryReview")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar lastSalaryReview;
      @XmlElementRef(name = "LeaveReason", namespace = "http://www.ultipro.com/contracts", type = JAXBElement.class)
      protected JAXBElement&lt;String&gt; leaveReason;
      @XmlElement(name = "NextPerfReview")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar nextPerfReview;
      @XmlElement(name = "NextSalaryReview")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar nextSalaryReview;
      @XmlElement(name = "OriginalHire")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar originalHire;
      @XmlElement(name = "PTOSuspendedFrom")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar ptoSuspendedFrom;
      @XmlElement(name = "PTOSuspendedTo")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar ptoSuspendedTo;
      @XmlElement(name = "PayAutomatically")
      protected Boolean payAutomatically;
      @XmlElement(name = "PaySuspendedFrom")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar paySuspendedFrom;
      @XmlElement(name = "PaySuspendedTo")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar paySuspendedTo;
      @XmlElementRef(name = "ROEIssueReason", namespace = "http://www.ultipro.com/contracts", type = JAXBElement.class)
      protected JAXBElement&lt;String&gt; roeIssueReason;
      @XmlElement(name = "RegularRetirement")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar regularRetirement;
      @XmlElementRef(name = "SelfServiceProperties", namespace = "http://www.ultipro.com/contracts", type = JAXBElement.class)
      protected JAXBElement&lt;ArrayOfKeyValueOfstringstring&gt; selfServiceProperties;
      @XmlElement(name = "Seniority")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar seniority;
      @XmlElement(name = "StatusAnticipatedEnd")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar statusAnticipatedEnd;
      @XmlElement(name = "StatusStartDate")
      @XmlSchemaType(name = "dateTime")
      protected XMLGregorianCalendar statusStartDate;
      @XmlElement(name = "Weeks")
      protected Double weeks;
  
  
  }
  </code></pre>