Created
November 15, 2012 09:31
-
-
Save jrudolph/4077644 to your computer and use it in GitHub Desktop.
json-lenses example
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
/.idea/ | |
/project/boot/ | |
/project/plugins/project | |
target/ | |
lib_managed/ | |
src_managed/ | |
test-output/ | |
*.iml |
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
sbt.version=0.12.1 |
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
libraryDependencies ++= Seq( | |
"io.spray" %% "spray-json" % "1.2.2", | |
"net.virtual-void" %% "json-lenses" % "0.5.1" | |
) |
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
import spray.json.{DefaultJsonProtocol, JsonParser} | |
import spray.json.lenses.JsonLenses._ | |
case class Bird(name: String, color: String) | |
case class Birds(german: String, birds: List[Bird]) | |
case class Ecosystem(ecoSystem: String, birds: Birds) | |
object BirdJsonProtocol extends DefaultJsonProtocol { | |
implicit val BirdFormat = jsonFormat(Bird, "name", "color") | |
implicit val BirdsFormat = jsonFormat(Birds, "german", "birds") | |
implicit val EcosystemFormat = jsonFormat(Ecosystem, "ecoSystem", "birds") | |
} | |
object ExtraPartOfJson extends App { | |
val jsonString = | |
"""{ | |
| "ecoSystem": "garden", | |
| "birds": { | |
| "german": "Voegel", | |
| "birds": [{ | |
| "name": "Amsel", | |
| "color": "black" | |
| }, { | |
| "name": "Bussard", | |
| "color": "brown" | |
| }] | |
| } | |
|}""".stripMargin | |
val json = JsonParser(jsonString) | |
println(json.prettyPrint) | |
import BirdJsonProtocol._ | |
// in spray-routing use: | |
// result.entity.as[Ecosystem] | |
val ecosystem = json.as[Ecosystem] | |
println(ecosystem) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment