aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats/json/studydesign.scala
blob: 60c7e70b697f0dd2341f716ad5589234c28938e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package xyz.driver.pdsuidomain.formats.json

import spray.json._
import xyz.driver.pdsuidomain.entities._

object studydesign {
  import DefaultJsonProtocol._
  import common._

  implicit val studyDesignFormat: RootJsonFormat[StudyDesign] = new RootJsonFormat[StudyDesign] {
    override def read(json: JsValue): StudyDesign = json match {
      case JsObject(fields) =>
        val name = fields
          .get("name")
          .map(_.convertTo[String])
          .getOrElse(deserializationError(s"Study design json object does not contain `name` field: $json"))

        StudyDesign
          .fromString(name)
          .getOrElse(deserializationError(s"Unknown study design: $name"))

      case _ => deserializationError(s"Expected Json Object as Study design, but got $json")
    }

    override def write(obj: StudyDesign) =
      JsObject("id" -> obj.id.toJson, "name" -> obj.name.toJson)
  }

}