Hey there, I am not able to parse my Json with Moshi when I define @Json(name).
Is it an expected behavior?
val moshi = Moshi.Builder().build()
val jsonAdapter = moshi.adapter<Route>(Route::class.java)
val route = jsonAdapter.fromJson(it)
{
"sourceLocation": [
-73.9654327,
40.7794406
]
}
data class Route(
var sourceLocation: List<Double>
)
import com.squareup.moshi.Json
data class Route(
@Json(name = "sourceLocation")
var originLocation: List<Double>
)
You need to target the annotation with @field:Json.
fun main(vararg args: String) {
val moshi = Moshi.Builder().build()
val adapter = moshi.adapter<Route>(Route::class.java)
val actual = adapter.fromJson("""
{
"sourceLocation": [
-73.9654327,
40.7794406
]
}
""".trimIndent())
println(actual)
}
data class Route(
@field:Json(name = "sourceLocation")
var originLocation: List<Double>
)
Route(originLocation=[-73.9654327, 40.7794406])
Or add the KotlinJsonAdapterFactory from the Kotlin artifact to your Moshi.Builder.
Most helpful comment
You need to target the annotation with
@field:Json.