blob: 5158dab0ee17de25ea79bef3dcf28e6f6fc2ce03 (
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
30
31
32
33
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
|