4

I've recently switched from Gson to Moshi and am having trouble parsing some Json.

{
  "access_token": "-LNe2LQ7DQH5Y2zs_W5iUumKuaUE",
  "token_type": "bearer",
  "device_id": "461f-837e-af5050c92fe9",
  "expires_in": 3600,
  "scope": "*"
}

And here's the model class:

data class AuthToken(
        @Json(name = "access_token") val accessToken: String,
        @Json(name = "token_type") val tokenType: String,
        @Json(name = "device_id") val deviceId: String,
        @Json(name = "expires_in") val expiresIn: Int,
        @Json(name = "scope") val scope: String
)

Whenever I switch to using Moshi in my retrofit client, I receive the following error:

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull

I have made the field nullable, however it is always deserialized as null. I've checked my retrofit response and it's (obviously) the same when using either Gson or Moshi. What am I doing wrong?

SpecialSnowflake
  • 945
  • 4
  • 16
  • 32

1 Answers1

4

For some reason, when I explicitly tell the AuthToken class to generate an adapter - I receive no null values.

@JsonClass(generateAdapter = true)
data class AuthToken(
        @Json(name = "access_token") val accessToken: String,
        @Json(name = "token_type") val tokenType: String,
        @Json(name = "device_id") val deviceId: String,
        @Json(name = "expires_in") val expiresIn: Int,
        @Json(name = "scope") val scope: String
)
SpecialSnowflake
  • 945
  • 4
  • 16
  • 32
  • 2
    If you don't use Moshi's Kotlin support (the reflective KotlinJsonAdapterFactory or codegen adapters), the `@Json` names are not read correctly. (They are not added to the field, and the default reflective Java adapter doesn't see them.) – Eric Cochran Nov 05 '18 at 19:38