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
val reads = __.json.update( // copies the full JSON | |
(__ \ "_id").json.copyFrom( (__ \ 'id).json.pick ) // adds _id | |
) andThen (__ \ "id").json.prune) // and after removes id |
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
test |
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 play.api.libs.json._ | |
val reads = (__ \ "foo").read[String] orElse Reads.pure("default string") |
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
<put your code here> |
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
case class Foo(bar: String) | |
// extends Function1 is just to workaround a limitation in Play2.1 | |
object Foo extends Function1(bar: String, Foo) { | |
implicit val fmt = Json.format[Foo] | |
} | |
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 play.api.libs.json._ | |
import play.api.libs.functional.syntax._ | |
val r = ( | |
(__ \ "field1").readNullable[String] and | |
(__ \ "field2").readNullable[Int] | |
).tupled.filter(ValidationError("unexpected result")){ | |
case( Some(x), None ) => true; | |
case ( None, Some(x) ) => true; | |
case _ => false |
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
<put your code here> |
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
// maybe we could add this one into Play... | |
// 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") |
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 play.api.libs.json._ | |
import play.api.data.validation._ | |
import play.api.libs.functional.syntax._ | |
val json = Json.obj( | |
"name" -> "John", | |
"email" -> "[email protected]", | |
"password" -> "password", | |
"confirmPassword" -> "password" | |
) |
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 play.api.libs.json._ | |
import play.api.libs.functional.syntax._ | |
implicit val cReads = Json.reads[Circle] | |
implicit val pReads = Json.reads[Polygon] | |
// you can replace __ (root node) by JsPath if you prefer | |
// | = orElse | |
// needs to convert to Form explicitly because Reads is invariant | |
implicit val shapeReads = __.read[Rect].map(x => x:Form) | __.read[Circle].map(x => x:Form) |