aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/CustomFormats.scala
blob: 3656e3b88eb6fe215c92ef5b00fe9814822a4789 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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"))
  }

}