title: Unmarshalling JSON array via Jettison/Resteasy
body: <p>Ran into a similar problem like the following forum post:</p>
  
  <p><a href="http://jersey.576304.n2.nabble.com/parsing-JSON-with-Arrays-using-Jettison-td5732207.html" rel="nofollow">http://jersey.576304.n2.nabble.com/parsing-JSON-with-Arrays-using-Jettison-td5732207.html</a></p>
  
  <p>Using Resteasy 2.0.1GA with Jettison 1.2 and getting a problem marshalling arrays when involving namespace mappings.  See code below.  Basically if the number of array entries are greater than one and namespace mappings are used.  Anybody else run into this problem?  The Nabble form poster got around it by writing a custom unmarshaller.</p>
  
  <p>I either need to isolate the Jettison bug or write a Resteasy extension of the JettisonMappedUnmarshaller class (which hands over the namespace mappings and unmarshaller to the Jettison Configuration).</p>
  
  <p>The following code doesn't unmarshall (post step) if the properties variables contains 2 or more entries.</p>
  
  <pre><code>
  public class Experimenting {
  
      @Path("test")
      public static class MyResource {
          @XmlAccessorType(XmlAccessType.FIELD)
          @XmlType(name = "Property", propOrder = { "name", "value" })
          public static class MyProperty {
              @XmlElement(name = "Name", required = true)
              protected String name;
              @XmlElement(name = "Value", required = true)
              protected String value;
  
              public String getName() {
                  return name;
              }
  
              public void setName(String name) {
                  this.name = name;
              }
  
              public String getValue() {
                  return value;
              }
  
              public void setValue(String value) {
                  this.value = value;
              }
          }
  
          @XmlType(name = "MyElement", propOrder = { "myProperty" })
          @XmlAccessorType(XmlAccessType.FIELD)
          @XmlRootElement(name = "MyElement", namespace = "http://www.klistret.com/cmdb/ci/commons")
          @Mapped(namespaceMap = { @XmlNsMap(namespace = "http://www.klistret.com/cmdb/ci/commons", jsonName = "com.klistret.cmdb.ci.commons") })
          public static class MyElement {
              @XmlElement(name = "MyProperty", namespace = "http://www.klistret.com/cmdb/ci/commons")
              protected List myProperty;
  
              public List getMyProperty() {
                  if (myProperty == null) {
                      myProperty = new ArrayList();
                  }
                  return this.myProperty;
              }
  
              public void setMyProperty(List myProperty) {
                  this.myProperty = myProperty;
              }
          }
  
          @GET
          @Path("myElement/{id}")
          @Produces(MediaType.APPLICATION_JSON)
          public MyElement getMy(@PathParam("id")
          Long id) {
              MyElement myElement = new MyElement();
  
              MyProperty example = new MyProperty();
              example.setName("example");
              example.setValue("of a property");
  
              MyProperty another = new MyProperty();
              another.setName("another");
              another.setValue("just a test");
  
              MyProperty[] properties = new MyProperty[] { example, another };
              myElement.setMyProperty(Arrays.asList(properties));
  
              return myElement;
          }
  
          @POST
          @Path("/myElement")
          @Consumes(MediaType.APPLICATION_JSON)
          @Produces(MediaType.APPLICATION_JSON)
          public MyElement createMy(MyElement myElement) {
              List properties = myElement.getMyProperty();
              System.out.println("Properties size: " + properties.size());
  
              return myElement;
          }
      }
  
      private Dispatcher dispatcher;
  
      @Before
      public void setUp() throws Exception {
          // embedded server
          dispatcher = MockDispatcherFactory.createDispatcher();
          dispatcher.getRegistry().addPerRequestResource(MyResource.class);
  
      }
  
      @Test
      public void getAndCreate() throws URISyntaxException,
              UnsupportedEncodingException {
          MockHttpRequest getRequest = MockHttpRequest.get("/test/element/44");
          MockHttpResponse getResponse = new MockHttpResponse();
  
          dispatcher.invoke(getRequest, getResponse);
          String getResponseBodyAsString = getResponse.getContentAsString();
  
          System.out.println(String.format(
                  "Get Response code [%s] with payload [%s]", getResponse
                          .getStatus(), getResponse.getContentAsString()));
  
          MockHttpRequest postRequest = MockHttpRequest.post("/test/element");
          MockHttpResponse postResponse = new MockHttpResponse();
  
          postRequest.contentType(MediaType.APPLICATION_JSON);
          postRequest.content(getResponseBodyAsString.getBytes("UTF-8"));
  
          dispatcher.invoke(postRequest, postResponse);
          System.out.println(String.format(
                  "Post Response code [%s] with payload [%s]", postResponse
                          .getStatus(), postResponse.getContentAsString()));
      }
  }
  </code></pre>