title: Use Moshi to deserialize generic type with child that can be of different types too
body: <p>I have to work with a server that sends these responses for any request:</p>
  <p>For OK:
  HTTP 200</p>
  <pre><code>{
      &quot;jsonrpc&quot;: &quot;2.0&quot;,
      &quot;id&quot;: null,
      &quot;result&quot;: {
          &quot;data&quot;: {
              &quot;exp&quot;: 1635637589,
              ...
              ...
          },
          &quot;success&quot;: true,
          &quot;errors&quot;: [],
          &quot;messages&quot;: []
      }
  }
  </code></pre>
  <p>For error:
  HTTP 200 (yes, and unfortunately that can't be changed)</p>
  <pre><code>{
      &quot;jsonrpc&quot;: &quot;2.0&quot;,
      &quot;id&quot;: null,
      &quot;result&quot;: {
          &quot;data&quot;: {},
          &quot;success&quot;: false,
          &quot;errors&quot;: [{
              &quot;code&quot;: 1001,
              &quot;message&quot;: &quot;Error&quot;
          }],
          &quot;messages&quot;: []
      }
  }
  </code></pre>
  <p>Notice that <code>data</code> is a json object of a specific type when the response is OK, and a different one when the response is an error. This format is used for all the responses, meaning that <code>data</code> can have different child fields.</p>
  <p>I want to use <code>Retrofit</code> + <code>Moshi</code> + <code>RxJava</code>, but I am struggling to find a way to deserialize the response to handle that <code>data</code> field using two different types. I have this model:</p>
  <pre><code>data class BaseResponse&lt;T&gt;(
      @Json(name = &quot;jsonrpc&quot;) val jsonrpc: String,
      @Json(name = &quot;id&quot;) val id: String?,
      @Json(name = &quot;result&quot;) val result: BaseResponseResult&lt;T&gt;
  )
  
  data class BaseResponseResult&lt;T&gt;(
      @Json(name = &quot;data&quot;) val data: T, // This is what I have a problem with
      @Json(name = &quot;success&quot;) val success: Boolean,
      @Json(name = &quot;errors&quot;) val errors: List&lt;Error&gt;
  )
  
  // This would be the data field
  data class LoginResponse(
      @Json(name = &quot;user_id&quot;) val userId: Long,
      ...
      ...
      ...
  )
  
  // This would be the data field
  data class ProfileResponse(
      @Json(name = &quot;name&quot;) val name: String,
      ...
      ...
      ...
  )
  </code></pre>
  <p>And this would be my Retrofit interface</p>
  <pre><code>interface UsersApi {
      @POST(&quot;api/login&quot;)
      fun loginReal(@Body request: BaseRequest&lt;LoginRequest&gt;): Single&lt;BaseResponse&lt;LoginResponse&gt;&gt;
  
      @POST(&quot;api/profile&quot;)
      fun loginReal(@Body request: BaseRequest&lt;ProfileRequest&gt;): Single&lt;BaseResponse&lt;ProfileResponse&gt;&gt;
  }
  </code></pre>
  <p>I thought about adding a custom deserializer to parse <code>BaseResponse&lt;T&gt;</code> and throw some exception in case the response was an error one, but I am not able to register a deserializer using generics. I have read Moshi's documentation and several posts about deserializers, but I can't get it to work with a generic. Is that possible with Moshi?</p>