【问题标题】:type mismatch error when creating Reads for Play 2.1创建 Reads for Play 2.1 时出现类型不匹配错误
【发布时间】:2023-03-17 16:01:02
【问题描述】:

我已经玩了好几个小时了。我一直在尝试不同的方法来创建阅读,但我完全被难住了。

我在 Play 2.1.0Scala 2.10.1

错误:

type mismatch; found : models.Registration.type required: play.api.libs.json.Reads[?]

代码:

package models

import play.api.libs.json._
import play.api.libs.functional.syntax._

case class Registration(
        user: (String,String,String,String,String,String)
)

object RegistrationHelper {
    implicit val regReads: Reads[Registration] = (
      (__ \ "user").read(
        (__ \ "id").read[String] and
        (__ \ "username").read[String] and
        (__ \ "first_name").read[String] and
        (__ \ "last_name").read[String] and
        (__ \ "email_address").read[String] and
        (__ \ "user_avatar").read[String]
        tupled
      )
    )(Registration) //!!ERROR ON THIS LINE
}

JSON:

{
  user: {
    id: "35fc8ba5-56c3-4ebe-9a21-489a1a207d2e",
    username: "flastname",
    first_name: "Firstname",
    last_name: "Lastname",
    email_address: "foo@bar.com",
    user_avatar: "http://blog.ideeinc.com/wp-content/uploads/2010/04/tineye-robot.jpg"
  }
}

【问题讨论】:

  • 您应该尝试升级到 2.1.1,有一些关于 Json 和伴随对象的修复(JSON 宏修复和改进)。并且始终使用Registration.apply _

标签: json scala playframework-2.1


【解决方案1】:

这应该可行:

implicit val regReads: Reads[Registration] = (__ \ "user").read(
    (__ \ "id").read[String] and
      (__ \ "username").read[String] and
      (__ \ "first_name").read[String] and
      (__ \ "last_name").read[String] and
      (__ \ "email_address").read[String] and
      (__ \ "user_avatar").read[String]
      tupled
  ) map Registration.apply _

有关更多信息,请参阅this 问题。

【讨论】:

  • 这看起来与所有文档完全不同...哈哈。但它似乎奏效了。
猜你喜欢
  • 1970-01-01
  • 2021-02-19
  • 2016-01-23
  • 2013-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多