如何编写Scala Play框架自定义class的多个JSON序列化器反序列化器?
How to write Scala Play framework custom class's multiple JSON serializer deserializer?
我有以下class需要序列化反序列化
case class User(userId: Int, userName: String, email: String,
password: String) {
def this() = this(0, "", "", "")
def this(userId: Int){
this(userId, "", "", "" )
}
def this(userId: Int, userName: String){
this(userId, userName, "", "" )
}
def this(userId: Int, userName: String, email: String){
this(userId, userName,email, "" )
}
def this(password: String){
this(0, "","", password )
}
}
我有多个构造函数的用户案例 class。这样用户就可以创建为
- var u = new User(mid, mname, memail, mpassword)
- var u = new User(mid, mname)
我期待 JSON 请求
JSON 请求类型 1:-
"teamMembers" : [ {
"userId" : 1,
"userName" : "user name",
"email" : "eamil",
"password" : "password"
}, {
"userId" : 2,
"userName" : "user name 2",
"email" : "email2",
"password" : "pssword"
} ]
OR JSON 请求类型 1:-
"teamMembers" : [ {
"userId" : 1,
"userName" : "user name"
}, {
"userId" : 2,
"userName" : "user name 2"
} ]
我实现的 JSON 序列化器反序列化器工作正常,如下所示仅适用于类型一的请求
trait UserJson extends Controller {
implicit val userWrites: Writes[User] = (
(__ \ "userId").write[Int] ~
(__ \ "userName").write[String] ~
(__ \ "email").write[String] ~
(__ \ "password").write[String]
)(unlift(User.unapply))
implicit val userReads: Reads[User] = (
(__ \ "userId").read[Int](min(0) keepAnd max(150)) ~
(__ \ "userName").read[String](minLength[String](2)) ~
(__ \ "email").read[String](minLength[String](2)) ~
(__ \ "password").read[String](minLength[String](2))
)(User.apply _)
}
但是对于类型 2 JSON 请求不起作用。你能告诉我如何为类型 2 JSON 请求补充吗?提前致谢!
您可以使用 Json Reads/Writes 而不是隐式值
例如:
Json.toJson(user)(userWrites1)
Json.toJson(user)(userWrites2)
听起来像 email
和 password
是 Optional
。您可以将 email
和 password
设置为 Option
并使用 writeNullable
,如下所示。那是你要找的吗?
旁注:您的 this
def
看起来很奇怪,在 Scala 中通常您使用 apply
来实现它。
case class User(userId: Int, userName: String, email: Option[String], password: Option[String])
implicit val userWrites: Writes[User] = (
(__ \ "userId").write[Int] ~
(__ \ "userName").write[String] ~
(__ \ "email").writeNullable[String] ~
(__ \ "password").writeNullable[String]
)(unlift(User.unapply))
我有以下class需要序列化反序列化
case class User(userId: Int, userName: String, email: String,
password: String) {
def this() = this(0, "", "", "")
def this(userId: Int){
this(userId, "", "", "" )
}
def this(userId: Int, userName: String){
this(userId, userName, "", "" )
}
def this(userId: Int, userName: String, email: String){
this(userId, userName,email, "" )
}
def this(password: String){
this(0, "","", password )
}
}
我有多个构造函数的用户案例 class。这样用户就可以创建为
- var u = new User(mid, mname, memail, mpassword)
- var u = new User(mid, mname)
我期待 JSON 请求
JSON 请求类型 1:-
"teamMembers" : [ {
"userId" : 1,
"userName" : "user name",
"email" : "eamil",
"password" : "password"
}, {
"userId" : 2,
"userName" : "user name 2",
"email" : "email2",
"password" : "pssword"
} ]
OR JSON 请求类型 1:-
"teamMembers" : [ {
"userId" : 1,
"userName" : "user name"
}, {
"userId" : 2,
"userName" : "user name 2"
} ]
我实现的 JSON 序列化器反序列化器工作正常,如下所示仅适用于类型一的请求
trait UserJson extends Controller {
implicit val userWrites: Writes[User] = (
(__ \ "userId").write[Int] ~
(__ \ "userName").write[String] ~
(__ \ "email").write[String] ~
(__ \ "password").write[String]
)(unlift(User.unapply))
implicit val userReads: Reads[User] = (
(__ \ "userId").read[Int](min(0) keepAnd max(150)) ~
(__ \ "userName").read[String](minLength[String](2)) ~
(__ \ "email").read[String](minLength[String](2)) ~
(__ \ "password").read[String](minLength[String](2))
)(User.apply _)
}
但是对于类型 2 JSON 请求不起作用。你能告诉我如何为类型 2 JSON 请求补充吗?提前致谢!
您可以使用 Json Reads/Writes 而不是隐式值
例如:
Json.toJson(user)(userWrites1)
Json.toJson(user)(userWrites2)
听起来像 email
和 password
是 Optional
。您可以将 email
和 password
设置为 Option
并使用 writeNullable
,如下所示。那是你要找的吗?
旁注:您的 this
def
看起来很奇怪,在 Scala 中通常您使用 apply
来实现它。
case class User(userId: Int, userName: String, email: Option[String], password: Option[String])
implicit val userWrites: Writes[User] = (
(__ \ "userId").write[Int] ~
(__ \ "userName").write[String] ~
(__ \ "email").writeNullable[String] ~
(__ \ "password").writeNullable[String]
)(unlift(User.unapply))