Skip to content

Instantly share code, notes, and snippets.

@mnn
Created April 1, 2017 05:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnn/3a932a94b63d0095ad817d85019fa894 to your computer and use it in GitHub Desktop.
Save mnn/3a932a94b63d0095ad817d85019fa894 to your computer and use it in GitHub Desktop.
Udash Option JSON codec
/**
* Udash Option JSON codec for (de)serializing it this way:
* Some(x) will be serialized as x.
* None will be serialized as null.
*/
implicit def optionCodec[T: GenCodec]: GenCodec[Option[T]] =
create[Option[T]](
i => i.inputType match {
case InputType.Null =>
i.readNull()
None
case _ =>
Some(read[T](i))
},
locally {
case (o, Some(t)) => write[T](o, t)
case (o, None) => o.writeNull()
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment