case类参数的值不是Serializable的成员

时间:2016-01-13 19:50:30

标签: scala intellij-idea playframework case-class

我正在尝试为Json结构构建一个验证器,此时我已经有了如下内容:

读取定义

case class SubTaskConfigElement(name: String)

case class MultiSelectConfig(subTasks: Seq[SubTaskConfigElement])

implicit val subTaskConfigElementReads: Reads[SubTaskConfigElement] =
    (__ \ "name").read[String](minLength[String](0)).map(SubTaskConfigElement)

implicit val multiSelectConfigReads: Reads[MultiSelectConfig] = (
    (__ \ "subTasks").read[Seq[SubTaskConfigElement]]
).map(MultiSelectConfig)

我的单元测试如下:

val configJson = Json.parse(
                """
                  |{
                  |    "subTasks": [
                  |        { "name": "Sub Task 1" },
                  |         { "name": "Sub Task 2" },
                  |        { "name": "Sub Task 3" }
                  |    ]
                  |}
                """.stripMargin)

            val valid = configJson.validate[MultiSelectConfig] getOrElse JsError

            logger.info(valid + "")

            valid must beAnInstanceOf[MultiSelectConfig]
            valid.subTasks must beAnInstanceOf[List[SubTaskConfigElement]]

在测试的最后一行,我在执行测试时遇到错误:

[error] /app/process-street/test/validation/widget/config/FormFieldWidgetSpec.scala:29: value subTasks is not a member of Serializable
[error]             valid.subTasks must beAnInstanceOf[Seq[SubTaskConfigElement]]

IntelliJ还将其识别为以下问题:"无法解析符号子任务"

为什么会这样?我错过了什么?

感谢。

1 个答案:

答案 0 :(得分:1)

问题是这一行:

val valid = configJson.validate[MultiSelectConfig] getOrElse JsError

valid的类型推断为Serializable,因为它是MultiSelectConfigJsError的常见父类型。两者都是case类,case类自动从`Serializable。

继承