On Wed, Jan 9, 2013 at 10:47 PM, Julien Richard-Foy <
notifications@github.com> wrote:

> In
> framework/src/play/src/main/scala/play/api/libs/json/JsConstraints.scala:
>
> >    def of[A](implicit r: Reads[A]) = r
> >
> > -  def optional[A](implicit reads:Reads[A]):Reads[Option[A]] =
> > +  /** deleted because useless and troublesome (better to use nullable anyway) */
> > +  //def optional[A](implicit reads:Reads[A]):Reads[Option[A]] =
> > +  //  Reads[Option[A]](js => JsSuccess(reads.reads(js).asOpt))
> > +
> > +  /** very simple optional field Reads that maps "null" to None */
> > +  def optionWithNull[T](implicit rds: Reads[T]): Reads[Option[T]] = Reads( js => js match {
> > +    case JsNull => JsSuccess(None)
> > +    case js => rds.reads(js).map(Some(_))
> > +  } )
>
> Note that this could be rewritten as follows for a better readability:
>
> def optionWithNull[T](implicit rds: Reads[T]): Reads[Option[T]] = Reads {
>   case JsNull => JsSuccess(None)
>   case js => rds.reads(js).map(Some(_))}
>
> You should also mention in the doc that if the field is missing from the
> JSON value this Reads will yield a JsError(…) rather than a
> JsSuccess(None)
>
It can't be missing as this is a Reads[T] on the value not on the JsPath.
So either it is JsNull, either it is not.



>  —
> Reply to this email directly or view it on GitHub<https://github.com/playframework/Play20/pull/662/files#r2597182>.
>
>