Created
May 20, 2014 20:20
-
-
Save seamusv/077f5c1cdd897ee8c7cb to your computer and use it in GitHub Desktop.
This file contains 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 play.api.libs.json.Json | |
/** | |
* Created by svenasse on 20/05/14. | |
*/ | |
object MyOptions extends App { | |
case class Person(name: String, age: Int) | |
val request = """{"name": "John Doe", "age": 24}""" | |
val jsonRequest = Json.parse(request) | |
val personOption = for { | |
name <- (jsonRequest \ "name").asOpt[String] | |
age <- (jsonRequest \ "age").asOpt[Int] | |
} yield Person(name, age) | |
val q = personOption.fold("Invalid person")(p => s"The name is ${p.name} aged ${p.age}") | |
println(q) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment