Skip to content

Instantly share code, notes, and snippets.

@dnephin
Last active January 1, 2016 22:39
Show Gist options
  • Save dnephin/8211419 to your computer and use it in GitHub Desktop.
Save dnephin/8211419 to your computer and use it in GitHub Desktop.
Play custom JSON BodyParser
object ApiBodyParser extends BodyParsers {
def apply[T](key: String)(implicit rds: Reads[T]): BodyParser[T] = parse.json.flatMap {
jsValue => (jsValue \ key).validate[T] match {
case JsSuccess(item, path) => {
BodyParser("api parser") {
request => Done(Right(item), Input.Empty)
}
}
// TODO: json error message with validation error
case e: JsError =>
parse.error(Future.successful(Results.BadRequest(e.toString)))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment