11package akka .http .scaladsl .marshallers .playjson
22
33import akka .http .scaladsl .marshalling .{Marshaller , ToEntityMarshaller }
4- import akka .http .scaladsl .model ._
54import akka .http .scaladsl .model .MediaTypes .`application/json`
5+ import akka .http .scaladsl .model ._
66import akka .http .scaladsl .server .{RejectionError , ValidationRejection }
77import akka .http .scaladsl .unmarshalling .{FromEntityUnmarshaller , Unmarshaller }
88import akka .util .ByteString
99import play .api .libs .json ._
1010
11- /** Automatic to and from JSON marshalling/unmarshalling using an in-scope
12- * *play-json* protocol.
13- */
11+ /**
12+ * Automatic to and from JSON marshalling/unmarshalling using an in-scope *play-json* protocol.
13+ */
1414trait PlayJsonSupport {
1515 import PlayJsonSupport ._
1616
@@ -19,7 +19,7 @@ trait PlayJsonSupport {
1919 .forContentTypes(`application/json`)
2020 .mapWithCharset {
2121 case (ByteString .empty, _) => throw Unmarshaller .NoContentException
22- case (data, charset) => data.decodeString(charset.nioCharset.name)
22+ case (data, charset) => data.decodeString(charset.nioCharset.name)
2323 }
2424
2525 private val jsonStringMarshaller : ToEntityMarshaller [String ] = {
@@ -37,45 +37,49 @@ trait PlayJsonSupport {
3737 }
3838 }
3939
40- /** HTTP entity => `A`
41- *
42- * @param reads
43- * reader for `A`
44- * @tparam A
45- * type to decode
46- * @return
47- * unmarshaller for `A`
48- */
49- implicit def playJsonUnmarshaller [A ](implicit
50- reads : Reads [A ]
40+ /**
41+ * HTTP entity => `A`
42+ *
43+ * @param reads
44+ * reader for `A`
45+ * @tparam A
46+ * type to decode
47+ * @return
48+ * unmarshaller for `A`
49+ */
50+ implicit def playJsonUnmarshaller [A ](
51+ implicit
52+ reads : Reads [A ],
5153 ): FromEntityUnmarshaller [A ] = {
5254 def read (json : JsValue ) = reads.reads(json) recoverTotal { error =>
5355 throw RejectionError (
5456 ValidationRejection (
5557 JsError .toJson(error).toString,
56- Some (PlayJsonError (error))
57- )
58+ Some (PlayJsonError (error)),
59+ ),
5860 )
5961 }
6062
6163 jsonStringUnmarshaller.map(data => read(Json .parse(data)))
6264 }
6365
64- /** `A` => HTTP entity
65- *
66- * @param writes
67- * writer for `A`
68- * @param printer
69- * output generation function, default to `stringify`, could be overridden
70- * with `Json.prettyPrint`
71- * @tparam A
72- * type to encode
73- * @return
74- * marshaller for any `A` value
75- */
76- implicit def playJsonMarshaller [A ](implicit
77- writes : Writes [A ],
78- printer : JsValue => String = Json .stringify
66+ /**
67+ * `A` => HTTP entity
68+ *
69+ * @param writes
70+ * writer for `A`
71+ * @param printer
72+ * output generation function, default to `stringify`, could be overridden with
73+ * `Json.prettyPrint`
74+ * @tparam A
75+ * type to encode
76+ * @return
77+ * marshaller for any `A` value
78+ */
79+ implicit def playJsonMarshaller [A ](
80+ implicit
81+ writes : Writes [A ],
82+ printer : JsValue => String = Json .stringify,
7983 ): ToEntityMarshaller [A ] = {
8084
8185 jsonStringMarshaller.compose(printer).compose(writes.writes)
@@ -87,7 +91,7 @@ object PlayJsonSupport extends PlayJsonSupport {
8791 val `application/json; charset=UTF-8` : ContentType .WithCharset =
8892 MediaType .customWithOpenCharset(
8993 " application" ,
90- " json"
94+ " json" ,
9195 ) withCharset HttpCharsets .`UTF-8`
9296
9397 case class PlayJsonError (error : JsError ) extends RuntimeException {
0 commit comments