Skip to content

Instantly share code, notes, and snippets.

@propensive
Created August 1, 2015 16:36
Show Gist options
  • Save propensive/dabb7816e83cc9f4da02 to your computer and use it in GitHub Desktop.
Save propensive/dabb7816e83cc9f4da02 to your computer and use it in GitHub Desktop.
Rapture JSON support for case class defaults
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.jawn._
import jsonBackends.jawn._ // This also works with Play, Spray, Argonaut, JSON4S and Lift
scala> val fruits = json"""[{ "name": "Tangerine" }, { "name": "Lemon", "color": "yellow" }, { "name": "Orange" }, { "name": "Peach", "citrus": false }]"""
fruits: rapture.json.Json = [{"name":"Tangerine"},{"color":"yellow","name":"Lemon"},{"name":"Orange"},{"name":"Peach","citrus":false}]
scala> case class Fruit(name: String, color: String = "orange", citrus: Boolean = true)
defined class Fruit
scala> fruits.as[Set[Fruit]]
res0: scala.collection.immutable.Set[Fruit] = Set(Fruit(Tangerine,orange,true), Fruit(Lemon,yellow,true), Fruit(Orange,orange,true), Fruit(Peach,orange,false))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment