Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
82 lines (68 sloc) 2.48 KB
package upickle.example
import acyclic.file
import utest._
import upickle.example.Simple.Thing
case class Opt(a: Option[String], b: Option[Int])
object Opt{
implicit def rw: OptionPickler.ReadWriter[Opt] = OptionPickler.macroRW
}
object OptionPickler extends upickle.AttributeTagged {
override implicit def OptionWriter[T: Writer]: Writer[Option[T]] =
implicitly[Writer[T]].comap[Option[T]] {
case None => null.asInstanceOf[T]
case Some(x) => x
}
override implicit def OptionReader[T: Reader]: Reader[Option[T]] =
implicitly[Reader[T]].mapNulls{
case null => None
case x => Some(x)
}
}
// end_ex
object OptionsAsNullTests extends TestSuite {
import OptionPickler._
implicit def rw: OptionPickler.ReadWriter[Thing] = OptionPickler.macroRW
val tests = TestSuite {
test("nullAsNone"){
// Quick check to ensure we didn't break anything
test("primitive"){
write("A String") ==> "\"A String\""
read[String]("\"A String\"") ==> "A String"
write(1) ==> "1"
read[Int]("1") ==> 1
write(Thing(1, "gg")) ==> """{"myFieldA":1,"myFieldB":"gg"}"""
read[Thing]("""{"myFieldA":1,"myFieldB":"gg"}""") ==> Thing(1, "gg")
}
test("none"){
write[None.type](None) ==> "null"
read[None.type]("null") ==> None
}
test("some"){
write(Some("abc")) ==> "\"abc\""
read[Some[String]]("\"abc\"") ==> Some("abc")
write(Some(1)) ==> "1"
read[Some[Int]]("1") ==> Some(1)
write(Some(3.14159)) ==> "3.14159"
read[Some[Double]]("3.14159") ==> Some(3.14159)
}
test("option"){
write(Option("abc")) ==> "\"abc\""
read[Option[String]]("\"abc\"") ==> Some("abc")
}
test("caseClass"){
write(Opt(None, None)) ==> """{"a":null,"b":null}"""
read[Opt]("""{"a":null,"b":null}""") ==> Opt(None, None)
write(Opt(Some("abc"), Some(1))) ==> """{"a":"abc","b":1}"""
}
test("optionCaseClass"){
implicit val thingReader = implicitly[Reader[Thing]]
implicit val thingWriter = implicitly[Writer[Thing]]
write(Opt(None, None)) ==> """{"a":null,"b":null}"""
read[Opt]("""{"a":null,"b":null}""") ==> Opt(None, None)
write(Opt(Some("abc"), Some(1))) ==> """{"a":"abc","b":1}"""
write(Option(Thing(1, "gg"))) ==> """{"myFieldA":1,"myFieldB":"gg"}"""
read[Option[Thing]]("""{"myFieldA":1,"myFieldB":"gg"}""") ==> Option(Thing(1, "gg"))
}
}
}
}
You can’t perform that action at this time.