I’m trying to serialize some relatively simple models into json. For example, I’d like to get the json representation of:
case class User(val id: Long, val firstName: String, val lastName: String, val email: Option[String]) {
def this() = this(0, "","", Some(""))
}
Do i need to write my own Format[User] with the appropriate reads and writes methods or is there some other way? I’ve looked at https://github.com/playframework/Play20/wiki/Scalajson but I’m still a bit lost.
Yes, writing your own
Formatinstance is the recommended approach. Given the following class, for example:The instance might look like this:
And you’d use it like this:
See the documentation for more information.