Created
August 1, 2015 16:49
-
-
Save propensive/f3bdc1fb8a97072eca58 to your computer and use it in GitHub Desktop.
Recursive case class extraction in Rapture JSON
This file contains hidden or 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
| 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