example: pickles for json extraction

19 views
Skip to first unread message

pelagic

unread,
Jun 29, 2014, 9:40:33 PM6/29/14
to dispatc...@googlegroups.com
I wanted to try out scala pickles for an upcoming project which I'm using Dispatch for so I put together a small example using pickles to extract json into case classes. It compiles and works, but I'm not sure if it's ready for prime time. I haven't tested it much or tried to check out the generation code. Anyway, I thought I'd post the example here in case anyone else was interested in pickles.

trait Service {

  val port: Option[Int]

  val host: String

  def service = port.map(p => :/(host, p)).getOrElse(:/(host))

  def extract[A : Unpickler : FastTypeTag](json: String) = json.unpickle[A]

  def get[A : Unpickler : FastTypeTag](path: String, params: (String, String)*): Future[A] = {
    val svc = service / path <<? Seq(params: _*)
    HttpHelper.executor(svc OK as.String).map(extract)
  }
}

And then to test

case class Foo(bar: String, baz: Baz)

case class Baz(id: Int)

val resp: Foo = TestService.extract[Foo]("""{"bar":"bar","baz":{"id":1}}""")

I have a small github project with one running test.
https://github.com/kaiserpelagic/DPickles
Reply all
Reply to author
Forward
0 new messages