0

I have following class need to serializer deserializer

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

I have User case class with multiple constructor. So that User can be created as

  1. var u = new User(mid, mname, memail, mpassword)
  2. var u = new User(mid, mname)

I am expecting JSON request as

JSON request type 1:-

 "teamMembers" : [ {
    "userId" : 1,
    "userName" : "user name",
    "email" : "eamil",
    "password" : "password"
  }, {
    "userId" : 2,
    "userName" : "user name 2",
    "email" : "email2",
    "password" : "pssword"
  } ]

OR JSON request type 1:-

"teamMembers" : [ {
    "userId" : 1,
    "userName" : "user name"
  }, {
    "userId" : 2,
    "userName" : "user name 2"
  } ]

My implemented JSON serializer deserializer which is working fine as follows only for type one request

  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 _)
} 

But for type 2 JSON request is not working. Could you please let me know how to plement for for type 2 JSON request? Thanks in advance!

2 Answers 2

1

You can use Json Reads/Writes not as implicit value

For example:

Json.toJson(user)(userWrites1)
Json.toJson(user)(userWrites2)
Sign up to request clarification or add additional context in comments.

2 Comments

what is userWrites1 and userWrites2? Is it something similar to my implemented userWrites or a new implementation or User class writter
userWrites1 is Writes for Json Request type 1
0

Sounds like email and password are Optional. you can set email and password as Option and use writeNullable as shown below. is that what you are looking for?

on a side note: your this defs are looking odd, in Scala generally you use apply for achieving that.

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))

1 Comment

I'm just learning scala. So don't know much. Could you please explain replace def this with apply. I need multiple constructor for User class. Could by all default value to null or 0 or only one of them provided. How to do it with apply? Thanks for the advise

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.