title: How to parse retrofit json response to object model with Moshi
body: <p>Retrofit Instance:</p>
  
  <pre><code>    fun getMoshi() : Moshi{
          return Moshi.Builder()
              .add(KotlinJsonAdapterFactory())
              .build()
      }
  
  
      fun retrofit(baseUrl: String): Retrofit = Retrofit.Builder()
          .client(getClient())
          .baseUrl(baseUrl)
          .addConverterFactory(MoshiConverterFactory.create(getMoshi()).asLenient())
          .build()
  
  }
  </code></pre>
  
  <p>BaseValidation  model:</p>
  
  <pre><code>    @JsonClass(generateAdapter = true)
      data class BaseValidation (
          val status : String,
          val data : List&lt;Data&gt;
      )
  </code></pre>
  
  <p>response parsing:</p>
  
  <pre><code>   val type: Type = Types.newParameterizedType(String::class.java, Data::class.java)
      val moshi = Moshi.Builder().build()
      val jsonAdapter: JsonAdapter&lt;Map&lt;String,Data&gt;&gt; = moshi.adapter(type)
      val baseValidation = jsonAdapter.fromJson(response.errorBody()!!.charStream().toString())!!
  </code></pre>
  
  <p>results in:</p>
  
  <pre><code>Platform class java.lang.String in java.lang.String&lt;com.example.vow.data_remote.model.create.response.validation.Data&gt; requires explicit JsonAdapter to be registered
  </code></pre>
  
  <p>I am new to Moshi and kind of confused how to parse correctly, I've managed to achieve the result with gson using code below, but I want to learn how it's done with Moshi:</p>
  
  <pre><code>val gson = Gson()
  val type = object : TypeToken&lt;BaseValidation&gt;() {}.type
  var errorResponse: BaseValidation? = gson.fromJson(response.errorBody()!!.charStream(), type)
  </code></pre>
  
  <p>is the Type below defined correctly?</p>
  
  <pre><code>val type: Type = Types.newParameterizedType(String::class.java, Data::class.java)
  </code></pre>
  
  <h2>UPDATE</h2>
  
  <pre><code>val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
  val jsonAdapter: JsonAdapter&lt;BaseValidation&gt; = moshi.adapter(BaseValidation::class.java).lenient()
  val baseValidation = jsonAdapter.fromJson(response.errorBody()!!.charStream().toString())!!
  </code></pre>
  
  <p>returns</p>
  
  <pre><code>com.squareup.moshi.JsonEncodingException: Use JsonReader.setLenient(true) to accept malformed JSON at path $
  </code></pre>
  
  <p>When moshi set to lenient() by</p>
  
  <pre><code>moshi.adapter(BaseValidation::class.java).lenient()
  </code></pre>
  
  <p>i get</p>
  
  <pre><code>Expected BEGIN_OBJECT but was STRING at path $
  </code></pre>
  
  <p>OkHttp response is </p>
  
  <pre><code>{
      "status": "validation",
      "data": [
          {
              "type": "Email",
              "code": 3000,
              "field": "User.contact.email",
              "message": "Email is invalid"
          }
      ]
  }
  </code></pre>
  
  <h2>SOLUTION</h2>
  
  <p>maybe it will help someone in the future</p>
  
  <pre><code> response.errorBody()!!.charStream().toString()
  </code></pre>
  
  <p>instead should be </p>
  
  <pre><code>response.errorBody()?.source()
  </code></pre>