Created
October 18, 2012 16:30
-
-
Save shairontoledo/3913014 to your computer and use it in GitHub Desktop.
Scala/Play jerkson parse to model
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
curl -v -XPOST -H "Content-Type: text/json" http://localhost:9000/invoices -d ' | |
{ | |
"name": "hashcode", | |
"other": "here", | |
"person": { | |
"name": "maria" | |
}, | |
"documents": [ | |
{ | |
"name": "foobar.pdf", | |
"size": 5555 | |
}, | |
{ | |
"name": "other file.doc", | |
"size": 4444444 | |
} | |
] | |
} | |
' |
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 com.codahale.jerkson.{Json => json} | |
case class Person(name: String) | |
case class Document(name: String, size: Int) | |
case class Invoice( | |
name: String, | |
other: Option[String], | |
person: Option[Person], | |
documents: Option[List[Document]] | |
) | |
def create = Action(parse.raw) { request => | |
val bytes = new String(request.body.asBytes().get) | |
val invoice = json.parse[Invoice](bytes) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment