public
Last active

Rapture I/O JSON extraction example

  • Download Gist
json.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
import rapture.io._
 
// Let's parse some JSON
val src: Json = Json.parse("""
{
"foo": "Hello world",
"bar": {
"baz": 42
}
}
""")
 
// This is the same as the JSON string literal:
val src: Json = json"""
{
"foo": "Hello world",
"bar": {
"baz": 42
}
}
"""
 
// We can now access the value bar.baz
val x: Json = src.bar.baz
 
// And get it as an integer
val y: Int = x.get[Int]
 
// Alternatively, we can use an extractor to get the values we want:
val json""" { "bar": { "baz": $x }, "foo": $z }""" = src
 
// Now x = 42 and z = "Hello world".

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.