diff options
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain')
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ListResponse.scala | 39 | ||||
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientcriterion.scala | 19 |
2 files changed, 42 insertions, 16 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ListResponse.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ListResponse.scala index 2a670c4..d294b38 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ListResponse.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ListResponse.scala @@ -10,7 +10,7 @@ final case class ListResponse[+T](items: Seq[T], meta: ListResponse.Meta) object ListResponse extends DefaultJsonProtocol { private val itemsField = "items" - private val metaField = "meta" + private val metaField = "meta" final case class Meta(itemsCount: Int, pageNumber: Int, pageSize: Int, lastUpdate: Option[LocalDateTime]) @@ -27,13 +27,40 @@ object ListResponse extends DefaultJsonProtocol { implicit val listResponseMetaFormat: RootJsonFormat[Meta] = jsonFormat4(Meta.apply) - implicit def listResponseMetaWriter[T: JsonWriter]: RootJsonWriter[ListResponse[T]] = + private def listResponseJsonWriter[T: JsonWriter](listResponse: ListResponse[T]) = { + JsObject( + itemsField -> listResponse.items.map(_.toJson).toJson, + metaField -> listResponse.meta.toJson + ) + } + + implicit def listResponseWriter[T: JsonWriter]: RootJsonWriter[ListResponse[T]] = new RootJsonWriter[ListResponse[T]] { override def write(listResponse: ListResponse[T]): JsValue = { - JsObject( - itemsField -> listResponse.items.map(_.toJson).toJson, - metaField -> listResponse.meta.toJson - ) + listResponseJsonWriter(listResponse) + } + } + + implicit def listResponseFormat[T: RootJsonFormat]: RootJsonFormat[ListResponse[T]] = + new RootJsonFormat[ListResponse[T]] { + override def write(listResponse: ListResponse[T]): JsValue = { + listResponseJsonWriter(listResponse) + } + + override def read(json: JsValue): ListResponse[T] = json match { + case JsObject(fields) => + val items = fields + .get(itemsField) + .map(_.convertTo[Seq[T]]) + .getOrElse(deserializationError(s"ListResponse json object does not contain `$itemsField` field: $json")) + + val meta = fields + .get(metaField) + .map(_.convertTo[Meta]) + .getOrElse(deserializationError(s"ListResponse json object does not contain `$metaField` field: $json")) + + ListResponse(items, meta) + case _ => deserializationError(s"Expected ListResponse json object, but got $json") } } } diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientcriterion.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientcriterion.scala index 7e8c220..f4d8e90 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientcriterion.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientcriterion.scala @@ -12,17 +12,16 @@ object patientcriterion { def applyUpdateToPatientCriterion(json: JsValue, orig: PatientCriterion): PatientCriterion = json match { case JsObject(fields) => - val eligibilityStatus = if (fields.contains("eligibilityStatus")) { - fields - .get("eligibilityStatus") - .map(_.convertTo[LabelValue]) - } else orig.eligibilityStatus + val eligibilityStatus = fields + .get("eligibilityStatus") + .map(_.convertTo[Option[LabelValue]]) + .getOrElse(orig.eligibilityStatus) + + val verifiedEligibilityStatus = fields + .get("verifiedEligibilityStatus") + .map(_.convertTo[Option[LabelValue]]) + .getOrElse(orig.verifiedEligibilityStatus) - val verifiedEligibilityStatus = if (fields.contains("verifiedEligibilityStatus")) { - fields - .get("verifiedEligibilityStatus") - .map(_.convertTo[LabelValue]) - } else orig.verifiedEligibilityStatus orig.copy( eligibilityStatus = eligibilityStatus, |