SpecialSnowflake
SpecialSnowflake

Reputation: 995

Moshi deserializing values as null

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?

Upvotes: 5

Views: 3229

Answers (1)

SpecialSnowflake
SpecialSnowflake

Reputation: 995

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
)

Upvotes: 4

Related Questions