Created
April 1, 2017 05:59
-
-
Save mnn/3a932a94b63d0095ad817d85019fa894 to your computer and use it in GitHub Desktop.
Udash Option JSON codec
This file contains 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
/** | |
* 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