diff options
author | vlad <vlad@driver.xyz> | 2017-07-20 10:37:08 -0700 |
---|---|---|
committer | vlad <vlad@driver.xyz> | 2017-07-20 10:37:08 -0700 |
commit | 5279d01cedb35a759347f194c0e8adb21d19e88e (patch) | |
tree | e340bb2eaba41d917bbca8c5e42b0b76bd164e37 /src/main/scala/xyz/driver/pdsuicommon/serialization/PlayJsonSupport.scala | |
parent | d9c3283c307105e03253c621b9b25a6308ac3b94 (diff) | |
parent | 7f7bd651122754a3df47894b64ddb0456561bbe7 (diff) | |
download | rest-query-5279d01cedb35a759347f194c0e8adb21d19e88e.tar.gz rest-query-5279d01cedb35a759347f194c0e8adb21d19e88e.tar.bz2 rest-query-5279d01cedb35a759347f194c0e8adb21d19e88e.zip |
Merge remote-tracking branch 'origin/master'
# Conflicts:
# build.sbt
# src/main/scala/xyz/driver/pdsuicommon/auth/AuthenticatedRequestContext.scala
# src/main/scala/xyz/driver/pdsuicommon/domain/User.scala
# src/main/scala/xyz/driver/pdsuidomain/formats/json/user/ApiPartialUser.scala
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuicommon/serialization/PlayJsonSupport.scala')
-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 |