Skip to content

Instantly share code, notes, and snippets.

@mandubian
Forked from playxamplez-admin/CODE
Last active December 20, 2015 03:09
Show Gist options
  • Save mandubian/6061873 to your computer and use it in GitHub Desktop.
Save mandubian/6061873 to your computer and use it in GitHub Desktop.
#Json #Reads a #JsArray & map on each elements of the array applying provided Reads (cumulative errors) #Play2.1
// Reads a JsArray and then map on its elements applying Reads (cumulating errors)
def readJsArrayMap[A <: JsValue](transformEach: Reads[A]): Reads[JsArray] = Reads { js => js match {
case arr: JsArray =>
arr.value.foldLeft(JsSuccess(Seq[JsValue]()): JsResult[Seq[JsValue]]) { (acc, e) =>
acc.flatMap{ seq =>
e.transform(transformEach).map( v => seq :+ v )
}
}.map(JsArray(_))
case _ => JsError("expected JsArray")
}}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2013 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment