jsonscaladeserializationliftjson-deserialization

json deserializer with support for parameterized Case Classes


Just encountered that liftweb.json does not work with parameterized Case Classes. The following fails at runtime:

case class ResponseOrError[R](status: String, responseData: Option[R], exception: Option[Error]) {
}

val answer = json.extract[ResponseOrError[Response]]

with:

do not know how to get type parameter from R

Is there any JSON deserializer, which actually works with parameterized Case Classes?


Solution

  • json4s works the way you expect. Here is an example:

    import org.json4s.{DefaultFormats, Formats}
    import org.json4s.jackson.JsonMethods.parse
    
    case class Z(str: String)
    case class X[R](z: Option[R])
    
    val json =
      """
        |{
        |    "z": {
        |        "str" : "test"
        |    }
        |}
      """.stripMargin
    
    implicit val formats: Formats = DefaultFormats.withStrictArrayExtraction
    
    val result = parse(json).extract[X[Z]]
    println(result)
    

    output

    X(Some(Z(test)))