type mismatch error when creating Reads for Play 2.1

混江龙づ霸主 提交于 2020-01-11 11:15:10

问题


I have been playing with this for hours. I have been trying different methods of creating a read and I am just completely stumped.

I am on Play 2.1.0 and Scala 2.10.1

ERROR:

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

Code:

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"
  }
}

回答1:


This should work:

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 _

See this question for more information.



来源:https://stackoverflow.com/questions/15800051/type-mismatch-error-when-creating-reads-for-play-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!