gpt4 book ai didi

scala - 找不到类型 List[(java.util.UUID, String, String, String, Int, Int, Int, Int, java.sql.Timestamp)] 的 Json 反序列化器

转载 作者:行者123 更新时间:2023-12-04 04:33:54 25 4
gpt4 key购买 nike

我缺乏 Scala 技能,但我一直在努力解决这个问题。但是我在序列化和反序列化 JSON 时遇到了问题。我用谷歌搜索并搜索了 StackOverflow,但不幸的是我无法将它拼凑在一起。

所以这是我最后的手段..

我的模型是:

package models

import java.util.UUID
import java.sql.Timestamp


import play.api.db._
import play.api.Play.current
import play.api.libs.json._


import slick.driver.PostgresDriver.simple._
import Database.threadLocalSession

case class User(
id:UUID,
username: String,
password: String,
email: String,
comment_score_down: Int,
comment_score_up: Int,
post_score_down: Int,
post_score_up: Int,
created_on: Timestamp)

object Users extends
Table[(UUID, String, String, String, Int, Int, Int, Int, Timestamp)]("users"){

implicit object UserFormat extends Format[User] {

implicit object UUIDFormatter extends Format[UUID] {
def reads(s: JsString): UUID = java.util.UUID.fromString(s.toString)
def writes(uuid: UUID) = JsString(uuid.toString)
}

implicit object TimestampFormatter extends Format[Timestamp] {
def reads(s: JsValue): Timestamp = new Timestamp(s.toString.toLong)
def writes(timestamp: Timestamp) = JsString(timestamp.toString)
}


def reads(json: JsValue): User = User(
(json \ "id").as[UUID],
(json \ "username").as[String],
(json \ "password").as[String],
(json \ "email").as[String],
(json \ "comment_score_down").as[Int],
(json \ "comment_score_up").as[Int],
(json \ "post_score_down").as[Int],
(json \ "post_score_up").as[Int],
(json \ "created_on").as[Timestamp]
)
def writes(u: User): JsValue = JsObject(List(
"id" -> JsString(u.id.toString),
"username" -> JsString(u.username),
"password" -> JsString(u.password),
"email" -> JsString(u.email),
"comment_score_down" -> JsString(u.comment_score_down.toString),
"comment_score_up" -> JsString(u.comment_score_up.toString),
"post_score_down" -> JsString(u.post_score_down.toString),
"post_score_up" -> JsString(u.post_score_up.toString),
"created_on" -> JsString(u.created_on.toString)
))
}


def id = column[UUID]("ID", O.PrimaryKey) // This is the primary key column
def username = column[String]("username")
def password = column[String]("password")
def email = column[String]("email")
def comment_score_down = column[Int]("comment_score_down")
def comment_score_up = column[Int]("comment_score_up")
def post_score_down = column[Int]("post_score_down")
def post_score_up = column[Int]("post_score_up")
def created_on = column[Timestamp]("created_on")

def * = id ~ username ~ password ~ email ~ comment_score_down ~
comment_score_up ~ post_score_down ~ post_score_up ~ created_on

}

我的 Controller :
  def getUsers = Action {
val json = database withSession {
val users = for (u <- Users) yield u.*
Json.toJson(users.list)
}
Ok(json).as(JSON)

}

谢谢你的时间!

最佳答案

凯,我觉得很甜蜜。

对我的模型进行了一些编辑:

case class User(
id:UUID,
username: String,
password: String,
email: String,
comment_score_down: Option[Int],
comment_score_up: Option[Int],
post_score_down: Option[Int],
post_score_up: Option[Int],
created_on: Timestamp)

object Users extends Table[User]("users"){

我还更改了我的对象签名,以便它可以返回类型 User 而不仅仅是 User 的参数。我只需要将 <> (User, User.unapply _) 附加到我的投影方法(*)。

但在我的 Controller 中:

我只需要:
  implicit object UserWrites extends Writes[User] {

def writes(u: User) = Json.obj(
"id" -> JsString(u.id.toString),
"username" -> JsString(u.username),
"password" -> JsString(u.password),
"email" -> JsString(u.email),
"comment_score_down" -> JsNumber(u.comment_score_down.getOrElse(0).toInt),
"comment_score_up" -> JsNumber(u.comment_score_up.getOrElse(0).toInt),
"post_score_down" -> JsNumber(u.post_score_down.getOrElse(0).toInt),
"post_score_up" -> JsNumber(u.post_score_up.getOrElse(0).toInt),
"created_on" -> JsString(u.created_on.toString)
)


}

作为 Controller 类的成员。

所以现在我的 Controller Action 只是:
  def getUsers = Action {

val json = database withSession {
val users = for (u <- Users) yield u
Json.toJson(users.list)
}
Ok(json).as(JSON)
}

编辑:

或者,我已经将 getUsers 代码作为 findAll 方法移到了我的模型中,并且也将我的可写对象移到了那里。我不喜欢 Controller 中的数据逻辑......

所以在我的 Controller 中我只有一个方法/ Action :
  def getUsers = Action {
Ok(Users.findAll).as(JSON)
}

我的模型现在看起来像:
package models

import java.util.UUID
import java.sql.Timestamp


import play.api.db._
import play.api.Play.current
import play.api.libs.json._


import slick.driver.PostgresDriver.simple._
import Database.threadLocalSession

case class User(
id:UUID,
username: String,
password: String,
email: String,
comment_score_down: Option[Int],
comment_score_up: Option[Int],
post_score_down: Option[Int],
post_score_up: Option[Int],
created_on: Timestamp)

object Users extends Table[User]("users") {

lazy val database = Database.forDataSource(DB.getDataSource())

def id = column[UUID]("id", O.PrimaryKey) // This is the primary key column
def username = column[String]("username")
def password = column[String]("password")
def email = column[String]("email")
def comment_score_down = column[Option[Int]]("comment_score_down")
def comment_score_up = column[Option[Int]]("comment_score_up")
def post_score_down = column[Option[Int]]("post_score_down")
def post_score_up = column[Option[Int]]("post_score_up")
def created_on = column[Timestamp]("created_on")

implicit object UserWrites extends Writes[User] {

def writes(u: User) = Json.obj(
"id" -> JsString(u.id.toString),
"username" -> JsString(u.username),
"password" -> JsString(u.password),
"email" -> JsString(u.email),
"comment_score_down" -> JsNumber(u.comment_score_down.getOrElse(0).toInt),
"comment_score_up" -> JsNumber(u.comment_score_up.getOrElse(0).toInt),
"post_score_down" -> JsNumber(u.post_score_down.getOrElse(0).toInt),
"post_score_up" -> JsNumber(u.post_score_up.getOrElse(0).toInt),
"created_on" -> JsString(u.created_on.toString)
)
}

def * = id ~ username ~ password ~ email ~ comment_score_down ~
comment_score_up ~ post_score_down ~ post_score_up ~ created_on <>
(User, User.unapply _)

def findByPK(pk: UUID) =
for (entity <- Users if entity.id === pk) yield entity

def findAll = database withSession {
val users = for (u <- Users) yield u
Json.toJson(users.list)
}

}

关于scala - 找不到类型 List[(java.util.UUID, String, String, String, Int, Int, Int, Int, java.sql.Timestamp)] 的 Json 反序列化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20097619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com