Created
August 11, 2010 07:13
-
-
Save debasishg/518613 to your computer and use it in GitHub Desktop.
This file contains 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 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