Skip to content

Instantly share code, notes, and snippets.

@seamusv
Created May 20, 2014 20:20
Show Gist options
  • Save seamusv/077f5c1cdd897ee8c7cb to your computer and use it in GitHub Desktop.
Save seamusv/077f5c1cdd897ee8c7cb to your computer and use it in GitHub Desktop.
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