No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator
Reason: This error occurs because jackson library
doesn't know how to create your model which doesn't have empty constructor
and the model contains constructor with parameters which didn't annotated its parameters with @JsonProperty("field_name")
. By default java compiler creates empty constructor if you didn't add constructor to your class.
Solution:Add empty constructor
to your model or annotate constructor parameters with @JsonProperty("field_name")
If you use kotlin data class
then also can annotate with @JsonProperty("field_name")
or register jackson module kotlin to ObjectMapper
.
You can create your models using http://www.jsonschema2pojo.org/.
I got here searching for this error:
No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator
Nothing to do with Retrofit but if you are using Jackson this error got solved by adding a default constructor to the class throwing the error.More here: https://www.baeldung.com/jackson-exception
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.