Using the Jackson (>2.0) library, I would like to deserialize data that is coming from a backend that I do not control into a single object that contains the id of the wrapper as well as all of the data within the raw JSON string that is contained in the wrapper. How would I write a custom Jackson deserializer to create a new object of Movie
without defining a wrapper class?
The data:
{
"id": "1",
"rawMovieData": "{\"name\": \"Office Space\", \"director\": \"Mike Judge\"}"
}
The data model:
case class Movie(id: String, name: String, director: String)
My current deserializer looks like this:
class MovieDeserializer extends JsonDeserializer[Movie] {
override def deserialize(jp: JsonParser, ctxt: DeserializationContext): Movie {
val wrapper: JsonNode = jp.getCodec.readValue(jp)
val id: String = wrapper.get("id").asInstanceOf[TextNode].textValue
val rawMovie: String = wrapper.get("rawMovieData").asInstanceOf[TextNode].textValue
//How do I now deserialize rawMovie?
Movie(id, name, director)
}
}
Note: My question is defined as Scala, but I think a Java approach would be similar enough as to not matter. So an answer in Java would be acceptable.