奇怪的通用方法行为

时间:2017-10-20 13:41:28

标签: scala json4s

这有效:

val rpcResponse = Serialization.read[RPCResponse[Map[String, Double]]](call("listaccounts"))

但是这个

val rpcResponse:RPCResponse[Map[String, Double]] = Serialization.read(call("listaccounts"))

给了我一个例外:

MappingException: Parsed JSON values do not match with class constructor args=
arg types=
executable=Executable(Constructor(public scala.runtime.Nothing$()))
cause=null
types comparison result=

这两种结构都不一样吗?

编辑:

案例类示例:

case class AA[R](f1: String, f2: R)

代码:

val rpcResponse: AA[Map[String, Double]] = Serialization.read(
  """
    |{
    | "f1": "fff",
    | "f2": {
    |       "a":1,
    |       "b":2
    |     }
    |}
  """.stripMargin)

println(rpcResponse)

1 个答案:

答案 0 :(得分:0)

据我了解,您需要指定默认的json formater。 下一个代码对我来说很好:

import org.json4s.native.Serialization  
implicit val formats = org.json4s.DefaultFormats

case class RPCResponse[R](f1:String, f2:R)

val rpcResponse:RPCResponse[Map[String, Double]] = Serialization.read[RPCResponse[Map[String, Double]]](
  """
    |{
    | "f1": "fff",
    | "f2": {
    |       "a":1,
    |       "b":2
    |     }
    |}
  """.stripMargin)

println(rpcResponse)