diff options
author | Jakob Odersky <jakob@odersky.com> | 2017-07-14 15:25:38 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-14 15:25:38 -0700 |
commit | dd9e22ac2e085505e2b6a1ca476fe1dcdc7e8618 (patch) | |
tree | da106c15b9402f3f6f9f19f92fe2a2258d5cf7d3 /src/main/scala/xyz/driver/pdsuicommon/serialization | |
parent | 3d902b5197db861c30325c159dc10cfb211ae209 (diff) | |
parent | d733294cb16fa83be014ce1efccbf364aa309d25 (diff) | |
download | rest-query-dd9e22ac2e085505e2b6a1ca476fe1dcdc7e8618.tar.gz rest-query-dd9e22ac2e085505e2b6a1ca476fe1dcdc7e8618.tar.bz2 rest-query-dd9e22ac2e085505e2b6a1ca476fe1dcdc7e8618.zip |
Merge pull request #7 from drivergroup/restification-tcv0.2.0
Implement trial curation rest services
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuicommon/serialization')
-rw-r--r-- | src/main/scala/xyz/driver/pdsuicommon/serialization/PlayJsonSupport.scala | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/pdsuicommon/serialization/PlayJsonSupport.scala b/src/main/scala/xyz/driver/pdsuicommon/serialization/PlayJsonSupport.scala new file mode 100644 index 0000000..5158dab --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuicommon/serialization/PlayJsonSupport.scala @@ -0,0 +1,34 @@ +package xyz.driver.pdsuicommon.serialization + +import akka.http.scaladsl.server.{RejectionError, ValidationRejection} +import akka.http.scaladsl.unmarshalling.Unmarshaller +import play.api.libs.json.{Reads, Writes} +import play.api.libs.json.Json +import akka.http.scaladsl.marshalling.ToEntityMarshaller +import akka.http.scaladsl.unmarshalling.FromEntityUnmarshaller +import akka.http.scaladsl.model.MediaTypes.`application/json` + +trait PlayJsonSupport { + import akka.http.scaladsl.marshalling.Marshaller + + implicit def playJsonUnmarshaller[A: Reads]: FromEntityUnmarshaller[A] = { + val reads = implicitly[Reads[A]] + Unmarshaller.stringUnmarshaller + .forContentTypes(`application/json`) + .map(Json.parse) + .map(reads.reads) + .map(_.recoverTotal { error => + throw RejectionError(ValidationRejection(s"Error reading JSON response as ${reads}.")) + }) + } + + implicit def playJsonMarshaller[A: Writes]: ToEntityMarshaller[A] = { + Marshaller + .stringMarshaller(`application/json`) + .compose(Json.prettyPrint) + .compose(implicitly[Writes[A]].writes) + } + +} + +object PlayJsonSupport extends PlayJsonSupport |