我的scala技能缺乏,但我一直在努力解决这个问题。但我在序列化和反序列化JSON时遇到问题。我用Google搜索并搜索了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
}
我的控制器:
def getUsers = Action {
val json = database withSession {
val users = for (u <- Users) yield u.*
Json.toJson(users.list)
}
Ok(json).as(JSON)
}
感谢您的时间!
答案 0 :(得分:4)
对我的模型进行了一些编辑:
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的参数。我只需附加&lt;&gt; (User,User.unapply _)到我的投影方法(*)。
但在我的控制器中:
我只是需要:
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 getUsers = Action {
val json = database withSession {
val users = for (u <- Users) yield u
Json.toJson(users.list)
}
Ok(json).as(JSON)
}
编辑:
或者,我已经将getUsers代码作为findAll方法移动到我的模型中,并且还将我的可写动作移到了那里。我不喜欢控制器中的数据逻辑...
所以在我的控制器中我只有一个方法/动作:
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)
}
}
答案 1 :(得分:-1)
看起来您的错误与您的写入方法有关。您已为User实现了反序列化器,但没有为User列表实现反序列化器。我能提供的最好的建议是创建一个参数类型为List [User]的写入方法。