title: Elegant solution to deserialize field that can be list or object
body: <p>I'm integrating with an API that responses with the follwing format:</p>
  
  <pre><code>{
      status: {
          status: 200,
          response_code: "SUCCESS",
          // ...
      },
      data: {
          //...
      }
  }
  </code></pre>
  
  <p>The <code>data</code> part can be an object or list of objects.
  To work with it I have created a nice generic class:</p>
  
  <pre><code>data class ApiResponseDTO&lt;T&gt; (
  
          @JsonProperty("status")
          val status: ResponseStatusDTO,
  
          @JsonProperty("data")
          val dataAny: T?
  )
  </code></pre>
  
  <p>Then I could use it in my Feign Client as:</p>
  
  <pre><code>@GetMapping("/foo")
  fun foo(): ApiResponse&lt;SomeDataObject&gt;
  
  @GetMapping("/bar")
  fun bar(): ApiResponse&lt;List&lt;SomeDataObject&gt;&gt;
  
  </code></pre>
  
  <p>Everything was working fine until I found an inconsistency in this API. When the requested endpoint doesn't return anything, it always return an empty array <code>[]</code>.
  So a request that returns something like:</p>
  
  <pre><code>// Foo response
  {
      status: {
          // ...
      },
      data: {
          foo: "bar"
      }
  }
  </code></pre>
  
  <p>Returns the following if it has no data to show:</p>
  
  <pre><code>// Foo response
  {
      status: {
          // ...
      },
      data: []
  }
  </code></pre>
  
  <p>This of course breaks my serialization/deserialization because I was always expecting a return of <code>ApiResponse&lt;FooObject&gt;</code> and suddenly I get a <code>ApiResponse&lt;List&lt;FooObject&gt;&gt;</code></p>
  
  <p>I'm looking for an elegant way to work around this scenario.
  I was thinking something like:</p>
  
  <pre><code>data class ApiResponseDTO&lt;T&gt; (
  
          @JsonProperty("status")
          val status: ResponseStatusDTO,
  
          @JsonSetter("data")
          val dataJson: JsonNode
  ) {
          @get:JsonGetter("data")
          val data: T?
                  get() {
                          val om = ObjectMapper()
                          return // Check wheter the JsonNode is an empty array and my T is not a Collection type and return null
                          // Otherwise, use ObjectMapper to deserialize the dataJson into my T object
                  }
  }
  </code></pre>
  
  <p>Problem is I cannot get <code>T</code> type at runtime because of the erasing, so I can't find a way to deserialize it with <code>ObjectMapper</code>.</p>
  
  <p>I also tried writing a custom <code>StdDeserializer</code> for the <code>data</code> field, but couldn't find a way to make it work with generics :s</p>
  
  <p>Thanks in advance</p>