Skip to content

Instantly share code, notes, and snippets.

View mandubian's full-sized avatar

Pascal Voitot mandubian

View GitHub Profile
@mandubian
mandubian / solution.scala
Last active December 14, 2015 09:09
How to write a #Json #Reads which replaces a key by another one and prunes the original branch ? #play2.1 #scala
val reads = __.json.update( // copies the full JSON
(__ \ "_id").json.copyFrom( (__ \ 'id).json.pick ) // adds _id
) andThen (__ \ "id").json.prune) // and after removes id
@mandubian
mandubian / code.scala
Last active December 14, 2015 09:18 — forked from gre/code
test
@mandubian
mandubian / CODE.scala
Last active December 14, 2015 09:29 — forked from zxamplez/CODE
#JSON Read a value and if failure, set default value #scala #Play2.1
import play.api.libs.json._
val reads = (__ \ "foo").read[String] orElse Reads.pure("default string")
@mandubian
mandubian / CODE
Created March 1, 2013 15:25 — forked from zxamplez/CODE
a
<put your code here>
@mandubian
mandubian / CODE.scala
Last active December 14, 2015 09:29 — forked from zxamplez/CODE
Workaround to create a #Play2.1 #Json #Reads / #Writes in a companion object #scala (in #Play2.2, it's no more needed)
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]
}
@mandubian
mandubian / CODE.scala
Last active December 14, 2015 09:29 — forked from zxamplez/CODE
#Play2.1 #Json Read 2 fields exclusively (accepts only one present) essaimodif #scala #nullable
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
@mandubian
mandubian / CODE
Created March 5, 2013 17:02 — forked from zxamplez/CODE
<put your code here>
@mandubian
mandubian / gist:5183939
Created March 17, 2013 22:34
Play2.1 JSON API: Reads a JsArray and then map on its elements applying Reads (cumulating errors)
// 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")
@mandubian
mandubian / gist:5231877
Last active December 15, 2015 08:38
Play Framework Json sample: Copy JsObject only if passwords equal
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"
)
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)