skip to Main Content

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:
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


  1. Observed some of the following issues with json modelling in OP.

    1. boundingBox: JsonNode and coursePoints: JsonNode is JsonNode but in json it is an array,
    2. course_points json does not contain any specific data type so assumed here as an empty array of type Any.
    3. I assume OP need to exclude 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

    data class BoundingBox(
      val lat: Int,
      val lng: Int
    )
    
    data class Route(
      @JsonProperty("bounding_box")
      val boundingBox: List<BoundingBox>,
      @JsonProperty("course_points")
      val coursePoints: List<Any>?,
      // val id: Int, // need to skip while reading/writing to json str
      val name: String
    )
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    data class RouteDetails(
      val route: Route,
      val type: String
    )
    

    Response json:
    Added additional property "someExtraKeys": "withExtraValue", to demonstrate that it is getting dropped while serialisation/deserialisation due to @JsonIgnoreProperties(ignoreUnknown = true)

    val response =
    """
      {
      "type": "route",
      "someExtraKeys": "withExtraValue",
      "route": {
        "id": 40307258,
        "name": "some bike route",
        "bounding_box": [
          {
            "lat": 3,
            "lng": -9
          },
          {
            "lat": 3,
            "lng": -9
          }
        ],
        "course_points": []
      }
    }
    """.trimIndent()
    

    Client code:

    val jacksonObjectMapper = jacksonObjectMapper()
    jacksonObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
    val readValue = jacksonObjectMapper.readValue(response, RouteDetails::class.java)
    println(jacksonObjectMapper.writeValueAsString(readValue))    
    /*
    {
      "route": {
        "bounding_box": [
          {
            "lat": 3,
            "lng": -9
          },
          {
            "lat": 3,
            "lng": -9
          }
        ],
        "course_points": [],
        "name": "some bike route"
      },
      "type": "route"
    }
    */  
    
    Login or Signup to reply.
  2. 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)

    Login or Signup to reply.
  3. 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:

    data class Response(
        val type: String
        val route: RouteDetails
    )
    

    Then you would parse the top-level object and get the route from there:

    val objectMapper = ObjectMapper()
    val response = objectMapper.readValue(response, Response::class.java)
    return response.route
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search