4

I was working with Moshi to pull JSON data from an API and map it to my DTO Data classes when I encountered this error:

FATAL EXCEPTION: main Process: com.plcoding.weatherapp, PID: 9706 com.squareup.moshi.JsonDataException: Required value 'weatherData' missing at $ at com.squareup.moshi.internal.Util.missingProperty(Util.java:649) at com.squareup.moshi.kotlin.reflect.KotlinJsonAdapter.fromJson(KotlinJsonAdapter.kt:103) at com.squareup.moshi.internal.NullSafeJsonAdapter.fromJson(NullSafeJsonAdapter.java:41)

My DTO are annotated with @field:Json(name = "xyz")

@JsonClass(generateAdapter = true)
data class WeatherDataDTO(

    @field:Json(name = "time")
    val times: List<String>,
    @field:Json(name = "temperature_2m")
    val temperatures: List<Double>)

I have enclosed the above DTO in another DTO.

JsonClass(generateAdapter = true)
data class WeatherDTO(

    @field:Json(name = "hourly")
    val weatherData: WeatherDataDTO

)

I am using the latest Retrofit and Moshi Libs on Kotlin 1.6.10

// Retrofit, Core Moshi JSON Library and Moshi's Kotlin support and converter factory
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation "com.squareup.moshi:moshi:1.12.0"
    implementation "com.squareup.moshi:moshi-kotlin:1.12.0"
    implementation 'com.squareup.retrofit2:converter-moshi:2.9.0'

My Json endpoint looks this:

{
"latitude": 42,
"longitude": 36,
"generationtime_ms": 0.3489255905151367,
"hourly": {
"time": [],
"temperature_2m": []
.....
}

Have struggled with this error for days but cannot find a solution.

Tonnie
  • 4,865
  • 3
  • 34
  • 50

2 Answers2

2

Removing @field:Json() in favor of just @Json(name = "xyz") did the trick for me.

My DTO now looks like this and is working fine!

 @JsonClass(generateAdapter = true)
    data class WeatherDataDTO(
    
        @Json(name = "time")
        val times: List<String>,
        @Json(name = "temperature_2m")
        val temperatures: List<Double>)

For more info on @field:Json(name = "xyz") vs @Json(name = "xyz") you can review this StackOverflow question.

I also found this article to be helpful in case the Moshi-JSON Exception persists.

Happy Coding ...

Tonnie
  • 4,865
  • 3
  • 34
  • 50
1

In my case I was migrating from gson to moshi and first I got the exception:

java.lang.IllegalArgumentException: Cannot serialize Kotlin type com.xxx.Spot. Reflective serialization of Kotlin classes without using kotlin-reflect has undefined and unexpected behavior. Please use KotlinJsonAdapter from the moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact.

Then I resolved it with adding @JsonClass(generateAdapter = true) on data class like your WeatherDataDTO that you did and right after that came the exception you mentioned in this post.

I have done what you posted in your answer, added the @Json(name) but because my data class fields that was getting the errors were null, I was getting that same error.

In my data class I had somewhat like this:

@JsonClass(generateAdapter = true)
    data class WeatherDataDTO(
    
        @Json(name = "time")
        val times: List<OtherDataClass>
    )
        

And the OtherDataClass:

@JsonClass(generateAdapter = true)
    data class OtherDataClass(
        @Json(name = "time")
        val problematicString: String, //This was causing the problem if it was null
            //and the String was non-nullable(without a "?" questionmark beside of String)
    )

What I did in OtherDataClass was making problematicString nullable:

@JsonClass(generateAdapter = true)
    data class OtherDataClass(
        @Json(name = "problemString")
        val problematicString: String? = null, // Made it nullable and initialized it as a null.
        //...other variables...
    )

Also I got the information from this stack overflow question

I hope this helps someone with the same exception.

John Xenakis
  • 211
  • 2
  • 8