title: JSON Unmarshalling of xs:string
body: <h3>Problem:</h3>
  
  <p>We are facing strange problems when marshalling JSONs objects including the following content {"@type":"xs:string"}. Marshalling of this object results in a NullPointerException. See the stack trace below:</p>
  
  <pre><code>java.lang.NullPointerException
  at com.sun.org.apache.xalan.internal.xsltc.trax.SAX2DOM.startElement(SAX2DOM.java:204)
  at com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler.closeStartTag(ToXMLSAXHandler.java:208)
  at com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler.characters(ToXMLSAXHandler.java:528)
  at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerHandlerImpl.characters(TransformerHandlerImpl.java:172)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.DomLoader.text(DomLoader.java:128)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.text(UnmarshallingContext.java:499)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.text(InterningXmlVisitor.java:78)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.processText(StAXStreamConnector.java:324)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleEndElement(StAXStreamConnector.java:202)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:171)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:355)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:334)
  at com.sun.jersey.json.impl.BaseJSONUnmarshaller.unmarshalJAXBElementFromJSON(BaseJSONUnmarshaller.java:108)
  at com.sun.jersey.json.impl.BaseJSONUnmarshaller.unmarshalFromJSON(BaseJSONUnmarshaller.java:97)
  at JerseyNPETest.testNPEUnmarshal(JerseyNPETest.java:20)
  </code></pre>
  
  <p>The problem occurs while getting the response from the external service and casting it implicity by glassfish (Simple REST call). </p>
  
  <p>We investigated the problem and found that it is actually related to the JSON unmarshaller. </p>
  
  <h3>Testcase:</h3>
  
  <p><strong>Marshalling</strong> - 
  To verify our finding, we created a class which contains a member of type Object named propertyA. Then we set the value of propertyA to "some value" and marshalled it using the default marshaller which results in the JSON string "{"@type":"xs:string","$":"some value"}". </p>
  
  <p><strong>Unmarshalling</strong> - Afterwards we used the default unmarsahller. The attempt to unmarshall this JSON string resulted in the mentioned exception. </p>
  
  <p>See the test case below:</p>
  
  <pre><code>import com.sun.jersey.api.json.JSONConfiguration;
  import com.sun.jersey.json.impl.BaseJSONUnmarshaller;
  import org.junit.Test;
  
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.JAXBException;
  import javax.xml.bind.annotation.XmlRootElement;
  import java.io.StringReader;
  
  public class JerseyNPETest {
  
  private static final String ERROR = "{\"additionalObject\":{\"@type\":\"xs:string\",\"$\":\"some value\"}}";
  
  @Test
  public void testNPEUnmarshal() throws JAXBException {
      final JAXBContext context = JAXBContext.newInstance(AnObject.class);
      final JSONConfiguration jsonConfig = JSONConfiguration.DEFAULT;
      final BaseJSONUnmarshaller unmarshaller = new BaseJSONUnmarshaller(context, jsonConfig);
      final StringReader reader = new StringReader(ERROR);
      final AnObject result = unmarshaller.unmarshalFromJSON(reader, AnObject.class);
  }
  
  @XmlRootElement
  public static class AnObject {
  
      private Object additionalObject;
  
      public Object getAdditionalObject() {
          return additionalObject;
      }
  
      public void setAdditionalObject(final Object additionalObject) {
          this.additionalObject = additionalObject;
      }
  }
  }
  </code></pre>
  
  <h3>Question:</h3>
  
  <p>How could this be solved in general e.g. by some configuration of glassfish to avoid this issue in the first place? 
  Currently we are working with glassfish 3.1.2.2. Any help is much appreciated!</p>