Created
April 20, 2012 04:55
-
-
Save leon/2426107 to your computer and use it in GitHub Desktop.
Json mapper with enum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Availability extends Enumeration { | |
val Free = Value("free") | |
val Busy = Value("busy") | |
val OOF = Value("oof") | |
val options = values.map(a => (a.toString -> a.toString.capitalize))(collection.breakOut) | |
} | |
case class User( | |
@Key("_id") id: ObjectId = new ObjectId, | |
guid: String, | |
name: String, | |
email: String, | |
phone: Option[String], | |
mobile: Option[String], | |
title: Option[String], | |
status: Availability.Value = Availability.Free, | |
returnTime: String, | |
office: String | |
){ | |
override def toString = guid + " - " + email | |
} | |
/* | |
Converts from json object to User | |
*/ | |
implicit object UserFormat extends Format[User] { | |
def reads(json: JsValue): User = new User( | |
new ObjectId, | |
(json \ "guid").as[String], | |
(json \ "name").as[String], | |
(json \ "email").as[String].toLowerCase, | |
(json \ "phone").asOpt[String], | |
(json \ "mobile").asOpt[String], | |
(json \ "title").asOpt[String], | |
(json \ "status").as[Availability], | |
(json \ "returnTime").as[String], | |
(json \ "office").as[String].toLowerCase | |
) | |
def writes(u: User): JsValue = JsObject(Seq( | |
"name" -> JsString(u.name), | |
"email" -> JsString(u.email) | |
)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment