Skip to content

Instantly share code, notes, and snippets.

@debasishg
Created August 11, 2010 07:13
Show Gist options
  • Save debasishg/518613 to your computer and use it in GitHub Desktop.
Save debasishg/518613 to your computer and use it in GitHub Desktop.
object Protocols {
import dispatch.json._
trait HttpType
implicit val HttpTypeFormat: Format[HttpType] = new Format[HttpType] {
def reads(json: JsValue): HttpType = json match {
case JsString("Get") => Get
case JsString("Post") => Post
case _ => error("Invalid HttpType")
}
def writes(a: HttpType): JsValue = a match {
case Get => JsString("Get")
case Post => JsString("Post")
}
}
case object Get extends HttpType
case object Post extends HttpType
case class Http(url: String, t: HttpType)
implicit val HttpFormat: Format[Http] =
asProduct2("url", "t")(Http)(Http.unapply(_).get)
}
// a sample test case
describe("Serialization with case objects") {
it("should serialize") {
import Protocols._
val h = Http("http://www.google.com", Get)
val h1 = fromjson[Http](tojson(h))
h1 should equal(h)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment