I’ve got a data class:
@JsonIgnoreProperties(ignoreUnknown = true)
data class RouteDetails(
val name: String,
val boundingBox: JsonNode,
val coursePoints: JsonNode
)
That I would like to deserialize a big json payload into (example is abbreviated):
{
"type": "route",
"route": {
"id": 40307258,
"name": "some bike route",
"bounding_box": [
{
"lat": 3
"lng": -9
},
{
"lat": 3,
"lng": -9
}
],
"course_points": [
{...}
]
}
}
And it seems like this should be a straight forward deserialization from what I can tell:
val objectMapper = ObjectMapper()
return objectMapper.readValue(response, RouteDetails::class.java)
But when I actually try this I get an error:
cannot deserialize from Object value (no delegate- or property-based Creator)
I’ve looked up examples and the documentation and I can’t tell what I’m doing wrong. I have a data class, I have the annotation to ignore the properties. Not seeing what I’m missing. Any help??
3
Answers
Observed some of the following issues with json modelling in OP.
boundingBox: JsonNode
andcoursePoints: JsonNode
is JsonNode but in json it is an array,course_points
json does not contain any specific data type so assumed here as an empty array of typeAny
.id
from the json, so added in json but not in DTO.I have modelled data class for the json mentioned in OP as below
Response json:
Added additional property
"someExtraKeys": "withExtraValue",
to demonstrate that it is getting dropped while serialisation/deserialisation due to@JsonIgnoreProperties(ignoreUnknown = true)
Client code:
Try adding the jackson-module-kotlin, this error means that Jackson can’t figure out how to use your constructor/"creator" (which it can’t by default for Kotlin data classes)
Your JSON payload does not match the data model.
What you showed is a JSON object that has a "route" object. So you need to model a data class that has a
RouteDetail
:Then you would parse the top-level object and get the route from there: