I am trying to serialise GeneralResponse
:
case class GeneralResponse[T](succeeded: Boolean, payload: Option[T])
and the payload is GroupsForUserResult
:
case class GroupsForUserResult(groups: Seq[UUID]).
I am using mapper.readValue(response.body, classOf[GeneralResponse[GroupsForUserResult]])
but unfortunately the payload is serialised as a Map
and not as the desired case class (GroupForUserResult
).
Because of Java Erasure - Jackson can't know at runtime about the generic type T from the line -
mapper.readValue(response.body, classOf[GeneralResponse[GroupsForUserResult]])
A solution to this problem will be
import com.fasterxml.jackson.core.`type`.TypeReference
mapper.readValue(json, new TypeReference[GeneralResponse[GroupsForUserResult]] {})
This way you provide an instance of TypeReference
with all the needed Type information.