Created
March 9, 2012 20:35
-
-
Save eamelink/2008543 to your computer and use it in GitHub Desktop.
Play2 Form handling of json with missing property vs null
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 org.specs2.mutable._ | |
import play.api.test._ | |
import play.api.test.Helpers._ | |
import play.api.data._ | |
import play.api.data.Forms._ | |
import play.api.libs.json.Json | |
class JsonBindingSpec extends Specification { | |
val missingProperty = Json parse """{}""" | |
val nullValue = Json parse """{"foo":null}""" | |
val stringValue = Json parse """{"foo":"bar"}""" | |
"The JSON form processor" should { | |
"bind a missing property to None" in { | |
form.bind(missingProperty).get must_== None | |
} | |
"bind a null value to Some(None)" in { | |
form.bind(nullValue).get must_== Some(None) | |
} | |
"bind a 'bar' value to Some(Some(bar))" in { | |
println(form.bind(stringValue).get) | |
form.bind(stringValue).get must_== Some(Some("bar")) | |
} | |
} | |
val form = Form( | |
single("foo" -> maybe(optional(text)))) | |
// This needs to be implemented in a way that it returns None on a missing property, | |
// but Some[A] when the property exists, even if the value is a null | |
def maybe[A](mapping: Mapping[A]): Mapping[Option[A]] = OptionalMapping(mapping) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment