7
val moshi = Moshi.Builder().build()
val type = Types.newParameterizedType(Map::class.java, String::class.java, Double::class.java)
val adapter = moshi.adapter<Map<String, Double>>(type)

val result = adapter.fromJson(InitData.json)

just switching from Gson to Moshi and i ran into this problem. I expect above code to work since it seems to work fine with Map<String, String>.

I'm getting an IllegalArgumentException: Unexpected primitive double. Use the boxed type

Same thing for Float. I know what's happening here but is this not possible with Moshi?

This is using com.squareup.moshi:moshi-kotlin:1.8.0

Flo We
  • 325
  • 2
  • 12

1 Answers1

20

Use Types.newParameterizedType(Map::class.java, String::class.java, Double::class.javaObjectType)

javaOjectType returns the corresponding boxed type for primitive types.

Eric Cochran
  • 8,414
  • 5
  • 50
  • 91
  • cool that works, was using `Any` and map to `Double` in the meantime, thanks! Didn't know about `javaObjectType` – Flo We Feb 20 '19 at 10:45