Skip to content

Instantly share code, notes, and snippets.

@leon
Created April 20, 2012 04:55
Show Gist options
  • Save leon/2426107 to your computer and use it in GitHub Desktop.
Save leon/2426107 to your computer and use it in GitHub Desktop.
Json mapper with enum
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