You need to use jackson-module-kotlin
to deserialize to data classes. See here for details.
The error message above is what Jackson gives you if you try to deserialize some value into a data class when that module isn't enabled or, even if it is, when the ObjectMapper
it uses doesn't have the KotlinModule
registered. For example, take this code:
data class TestDataClass (val foo: String)
val jsonString = """{ "foo": "bar" }"""
val deserializedValue = ObjectMapper().readerFor(TestDataClass::class.java).readValue<TestDataClass>(jsonString)
This will fail with the following error:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `test.SerializationTests$TestDataClass` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
If you change the code above and replace ObjectMapper
with jacksonObjectMapper
(which simply returns a normal ObjectMapper
with the KotlinModule
registered), it works. i.e.
val deserializedValue = jacksonObjectMapper().readerFor(TestDataClass::class.java).readValue<TestDataClass>(jsonString)
I'm not sure about the Android side of things, but it looks like you'll need to get the system to use the jacksonObjectMapper
to do the deserialization.