title: Jersey, Jackson and JAX-RS POST multiple JSON formats
body: <p>I am trying to be able to define the following code:</p>
  
  <pre><code>public class MyObject {
      private String name;
      ... // Other attributes
  }
  
  @Path(...)
  @Stateless
  public class MyRestResource {
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      public Response create(List&lt;MyObject&gt; myObjects) {
          // Do some stuff there
      }
  }
  </code></pre>
  
  <p>I know that I need to use:</p>
  
  <pre><code>DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true
  </code></pre>
  
  <p>to setup correctly my object mapper to be able to accept single value as array on my rest resources. I succeed to setup that part.</p>
  
  <p>My problem with this approach is that the following content is not differentiable:</p>
  
  <pre><code>{
       "name": "a name",
       ... // other attributes
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>[{
       "name": "a name",
       ... // other attributes
  }]
  </code></pre>
  
  <p>will result into a list (List) of size one. Then, in the method create(List myObjects), I will not be able to do the difference between the List and the Single Object sent to the Rest Resource.</p>
  
  <p>Then, my question is how to do something like that. The idea is to have only one @POST that accepts both Arrays and Single values?</p>
  
  <p>Ideally, I will get rid of the configuration of the ObjectMapper to avoid letting the possibility to set Single Object into the other level of the JSON document. For example, I do not want to allow that:</p>
  
  <pre><code>{
      ...
      "attributes": {
           ...
      }
  }
  </code></pre>
  
  <p>where normally this format should be mandatory:</p>
  
  <pre><code>{
      ...
      "attributes": [{
           ...
      }]
  }
  </code></pre>
  
  <p>Based on that, I tried to put in place an object wrapper of my List to set if I am able to the difference between the list and the object. With something like that:</p>
  
  <pre><code>public class ObjectWrapper&lt;T&gt; {
      private List&lt;T&gt; list;
      private T object;
  
      public boolean isObject() {
          return list == null;
      }
  }
  </code></pre>
  
  <p>with the resource that becomes:</p>
  
  <pre><code>@Path(...)
  @Stateless
  public class MyRestResource {
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      public Response create(ObjectWrapper myObjects) {
          // Do some stuff there
      }
  }
  </code></pre>
  
  <p>and trying to put in place the deserialization of my content through the JAX-RS/Jersey/Jackson mechanisms. If I let the solution as it is now, the deserialization fails due to the fact that the JSON format expected is the following:</p>
  
  <pre><code>{
       "list": [{
           "name": "a name",
           ... // other attributes
       }]
  }
  </code></pre>
  
  <p>Then I tried to write a custom deserializer but I am a bit lost in this task. I have something like that:</p>
  
  <pre><code>public class ObjectWrapperDeserializer&lt;T&gt; extends JsonDeserializer&lt;T&gt; {
      @Override
      public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
          ... // What to put there to deserialize Array or Object
      }
  }
  </code></pre>
  
  <p>I just want to deserialize the root level to set the content deserialized into the object wrapper. I also want to keep the feature configured in a class annotated with @ApplicationPath when the configuraiton of the different @Provider are done.</p>
  
  <p>I hope that all the info will give a sufficient picture of what I want to do and what I already tested.</p>
  
  <p>Waiting for suggestion on how to do a resource that accept Arrays or Objects on the same path.</p>
  
  <p>Thanks a lot in advance.</p>