Skip to content

Instantly share code, notes, and snippets.

@propensive
Created August 1, 2015 16:49
Show Gist options
  • Save propensive/f3bdc1fb8a97072eca58 to your computer and use it in GitHub Desktop.
Save propensive/f3bdc1fb8a97072eca58 to your computer and use it in GitHub Desktop.
Recursive case class extraction in Rapture JSON
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import rapture.json._
import rapture.json._
scala> import jsonBackends.spray._
import jsonBackends.spray._
scala> val order = json"""{ "name": "Jon Pretty", "items": [{ "name": "Apple", "quantity": 2 }, { "name": "Turnip" }] }"""
order: rapture.json.Json = {"name":"Jon Pretty","items":[{"name":"Apple","quantity":2},{"name":"Turnip"}]}
scala> case class OrderItem(name: String, quantity: Int = 1)
defined class OrderItem
scala> case class Order(name: String, items: Vector[OrderItem])
defined class Order
scala> order.as[Order]
res0: Order = Order(Jon Pretty,Vector(OrderItem(Apple,2), OrderItem(Turnip,1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment