diff options
author | Jakob Odersky <jakob@odersky.com> | 2018-02-13 15:09:31 -0800 |
---|---|---|
committer | Jakob Odersky <jakob@odersky.com> | 2018-02-13 15:35:12 -0800 |
commit | af6845cc08ae223ad67ebad61559d8264fd3346b (patch) | |
tree | 0983ef274c10eaacb0304db65e5217ec3fd00816 /src/main/scala/CustomFormats.scala | |
download | spray-json-derivation-af6845cc08ae223ad67ebad61559d8264fd3346b.tar.gz spray-json-derivation-af6845cc08ae223ad67ebad61559d8264fd3346b.tar.bz2 spray-json-derivation-af6845cc08ae223ad67ebad61559d8264fd3346b.zip |
Initial commit
Diffstat (limited to 'src/main/scala/CustomFormats.scala')
-rw-r--r-- | src/main/scala/CustomFormats.scala | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/main/scala/CustomFormats.scala b/src/main/scala/CustomFormats.scala new file mode 100644 index 0000000..3656e3b --- /dev/null +++ b/src/main/scala/CustomFormats.scala @@ -0,0 +1,19 @@ + +import spray.json._ + +trait CustomFormats extends DefaultJsonProtocol { + + implicit val fooFormat: JsonFormat[Foo] = new JsonFormat[Foo] { + def read(number: JsValue) = number match { + case JsNumber(x) => Foo(-x.toInt) + case tpe => sys.error(s"no way I'm reading that type $tpe!") + } + def write(number: Foo) = JsNumber(-number.x) + } + + implicit val z: JsonFormat[B] = new JsonFormat[B] { + def read(x: JsValue) = B("gone") + def write(x: B) = JsObject("a" -> JsString("A")) + } + +} |