/
Writes.scala
436 lines (368 loc) · 13 KB
/
Writes.scala
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
* Copyright (C) 2009-2016 Lightbend Inc. <https://www.lightbend.com>
*/
package play.api.libs.json
import java.time.format.DateTimeFormatter
import java.time.temporal.Temporal
import java.time.{ Instant, LocalDate, LocalDateTime, OffsetDateTime, ZoneOffset, ZonedDateTime }
import com.fasterxml.jackson.databind.JsonNode
import play.api.libs.functional.ContravariantFunctor
import play.api.libs.json.Json._
import play.api.libs.json.jackson.JacksonJson
import scala.annotation.implicitNotFound
import scala.collection._
import scala.reflect.ClassTag
/**
* Json serializer: write an implicit to define a serializer for any type
*/
@implicitNotFound(
"No Json serializer found for type ${A}. Try to implement an implicit Writes or Format for this type."
)
trait Writes[-A] {
/**
* Convert the object into a JsValue
*/
def writes(o: A): JsValue
/**
* Transforms the resulting [[JsValue]] using transformer function
*/
def transform(transformer: JsValue => JsValue): Writes[A] = Writes[A] { a => transformer(this.writes(a)) }
/**
* Transforms resulting [[JsValue]] using Writes[JsValue]
*/
def transform(transformer: Writes[JsValue]): Writes[A] = Writes[A] { a => transformer.writes(this.writes(a)) }
}
@implicitNotFound(
"No Json serializer as JsObject found for type ${A}. Try to implement an implicit OWrites or OFormat for this type."
)
trait OWrites[-A] extends Writes[A] {
def writes(o: A): JsObject
/**
* Transforms the resulting [[JsValue]] using transformer function
*/
def transform(transformer: JsObject => JsObject): OWrites[A] =
OWrites[A] { a => transformer(this.writes(a)) }
/**
* Transforms resulting [[JsValue]] using Writes[JsValue]
*/
def transform(transformer: OWrites[JsObject]): OWrites[A] =
OWrites[A] { a => transformer.writes(this.writes(a)) }
}
object OWrites extends PathWrites with ConstraintWrites {
import play.api.libs.functional._
implicit val functionalCanBuildOWrites: FunctionalCanBuild[OWrites] = new FunctionalCanBuild[OWrites] {
def apply[A, B](wa: OWrites[A], wb: OWrites[B]): OWrites[A ~ B] = OWrites[A ~ B] { case a ~ b => wa.writes(a).deepMerge(wb.writes(b)) }
}
implicit val contravariantfunctorOWrites: ContravariantFunctor[OWrites] = new ContravariantFunctor[OWrites] {
def contramap[A, B](wa: OWrites[A], f: B => A): OWrites[B] = OWrites[B](b => wa.writes(f(b)))
}
def apply[A](f: A => JsObject): OWrites[A] = new OWrites[A] {
def writes(a: A): JsObject = f(a)
}
}
/**
* Default Serializers.
*/
object Writes extends PathWrites with ConstraintWrites with DefaultWrites {
val constraints: ConstraintWrites = this
val path: PathWrites = this
implicit val contravariantfunctorWrites: ContravariantFunctor[Writes] = new ContravariantFunctor[Writes] {
def contramap[A, B](wa: Writes[A], f: B => A): Writes[B] = Writes[B](b => wa.writes(f(b)))
}
def apply[A](f: A => JsValue): Writes[A] = new Writes[A] {
def writes(a: A): JsValue = f(a)
}
}
/**
* Default Serializers.
*/
trait DefaultWrites {
import scala.language.implicitConversions
/**
* Serializer for Int types.
*/
implicit object IntWrites extends Writes[Int] {
def writes(o: Int) = JsNumber(o)
}
/**
* Serializer for Short types.
*/
implicit object ShortWrites extends Writes[Short] {
def writes(o: Short) = JsNumber(o)
}
/**
* Serializer for Byte types.
*/
implicit object ByteWrites extends Writes[Byte] {
def writes(o: Byte) = JsNumber(o)
}
/**
* Serializer for Long types.
*/
implicit object LongWrites extends Writes[Long] {
def writes(o: Long) = JsNumber(o)
}
/**
* Serializer for Float types.
*/
implicit object FloatWrites extends Writes[Float] {
def writes(o: Float) = JsNumber(o)
}
/**
* Serializer for Double types.
*/
implicit object DoubleWrites extends Writes[Double] {
def writes(o: Double) = JsNumber(o)
}
/**
* Serializer for BigDecimal types.
*/
implicit object BigDecimalWrites extends Writes[BigDecimal] {
def writes(o: BigDecimal) = JsNumber(o)
}
/**
* Serializer for Boolean types.
*/
implicit object BooleanWrites extends Writes[Boolean] {
def writes(o: Boolean) = JsBoolean(o)
}
/**
* Serializer for String types.
*/
implicit object StringWrites extends Writes[String] {
def writes(o: String) = JsString(o)
}
/**
* Serializer for Jackson JsonNode
*/
implicit object JsonNodeWrites extends Writes[JsonNode] {
def writes(o: JsonNode): JsValue = JacksonJson.jsonNodeToJsValue(o)
}
/**
* Serializer for Array[T] types.
*/
implicit def arrayWrites[T: ClassTag: Writes]: Writes[Array[T]] = Writes[Array[T]] { ts =>
JsArray(ts.map(toJson(_)).toSeq)
}
/**
* Serializer for Map[String,V] types.
*/
implicit def mapWrites[V: Writes]: OWrites[Map[String, V]] = OWrites[Map[String, V]] { ts =>
JsObject(ts.mapValues(toJson(_)).toSeq)
}
/**
* Serializer for Traversables types.
*/
implicit def traversableWrites[A: Writes] = Writes[Traversable[A]] { as =>
JsArray(as.map(toJson(_)).toSeq)
}
/**
* Serializer for JsValues.
*/
implicit object JsValueWrites extends Writes[JsValue] {
def writes(o: JsValue) = o
}
/**
* Serializer for Option.
*/
implicit def OptionWrites[T](implicit fmt: Writes[T]): Writes[Option[T]] = new Writes[Option[T]] {
def writes(o: Option[T]) = o match {
case Some(value) => fmt.writes(value)
case None => JsNull
}
}
/**
* Serializer for java.util.Date
* @param pattern the pattern used by SimpleDateFormat
*/
def dateWrites(pattern: String): Writes[java.util.Date] = new Writes[java.util.Date] {
def writes(d: java.util.Date): JsValue = JsString(new java.text.SimpleDateFormat(pattern).format(d))
}
/**
* Default Serializer java.util.Date -> JsNumber(d.getTime (nb of ms))
*/
implicit object DefaultDateWrites extends Writes[java.util.Date] {
def writes(d: java.util.Date): JsValue = JsNumber(d.getTime)
}
/** Typeclass to implement way of formatting of Java8 temporal types. */
trait TemporalFormatter[T <: Temporal] {
def format(temporal: T): String
}
/** Formatting companion */
object TemporalFormatter {
implicit def DefaultLocalDateTimeFormatter(formatter: DateTimeFormatter): TemporalFormatter[LocalDateTime] = new TemporalFormatter[LocalDateTime] {
def format(temporal: LocalDateTime): String = formatter.format(temporal)
}
implicit def PatternLocalDateTimeFormatter(pattern: String): TemporalFormatter[LocalDateTime] =
DefaultLocalDateTimeFormatter(DateTimeFormatter.ofPattern(pattern))
implicit def DefaultOffsetDateTimeFormatter(formatter: DateTimeFormatter): TemporalFormatter[OffsetDateTime] = new TemporalFormatter[OffsetDateTime] {
def format(temporal: OffsetDateTime): String = formatter.format(temporal)
}
implicit def PatternOffsetDateTimeFormatter(pattern: String): TemporalFormatter[OffsetDateTime] =
DefaultOffsetDateTimeFormatter(DateTimeFormatter.ofPattern(pattern))
implicit def DefaultZonedDateTimeFormatter(formatter: DateTimeFormatter): TemporalFormatter[ZonedDateTime] = new TemporalFormatter[ZonedDateTime] {
def format(temporal: ZonedDateTime): String = formatter.format(temporal)
}
implicit def PatternZonedDateTimeFormatter(pattern: String): TemporalFormatter[ZonedDateTime] =
DefaultZonedDateTimeFormatter(DateTimeFormatter.ofPattern(pattern))
implicit def DefaultDateFormatter(formatter: DateTimeFormatter): TemporalFormatter[LocalDate] = new TemporalFormatter[LocalDate] {
def format(temporal: LocalDate): String = formatter.format(temporal)
}
implicit def PatternDateFormatter(pattern: String): TemporalFormatter[LocalDate] =
DefaultDateFormatter(DateTimeFormatter.ofPattern(pattern))
implicit def DefaultInstantFormatter(formatter: DateTimeFormatter): TemporalFormatter[Instant] = new TemporalFormatter[Instant] {
def format(temporal: Instant): String = formatter.format(temporal)
}
implicit def PatternInstantFormatter(pattern: String): TemporalFormatter[Instant] =
DefaultInstantFormatter(DateTimeFormatter.ofPattern(pattern).withZone(ZoneOffset.UTC))
}
/**
* Serializer for Java8 temporal types (e.g. `java.time.LocalDateTime`)
* to be written as JSON string.
*
* @tparam A the Java8 temporal type to be considered: LocalDateTime, ZonedDateTime, Instant
* @tparam B Type of formatting argument
*
* @param formatting an argument to instantiate formatter
*
* {{{
* import java.time.LocalDateTime
* import play.api.libs.json.Writes
*
* implicit val temporalWrites: Writes[LocalDateTime] =
* temporalWrites[LocalDateTime, DateTimeFormatter](
* DateTimeFormatter.ISO_LOCAL_DATE_TIME)
* }}}
*/
def temporalWrites[A <: Temporal, B](formatting: B)(implicit f: B => TemporalFormatter[A]): Writes[A] = new Writes[A] {
def writes(temporal: A): JsValue = JsString(f(formatting) format temporal)
}
/**
* The default typeclass to write a `java.time.LocalDateTime`,
* using '2011-12-03T10:15:30' format.
*/
implicit val DefaultLocalDateTimeWrites =
temporalWrites[LocalDateTime, DateTimeFormatter](
DateTimeFormatter.ISO_LOCAL_DATE_TIME)
/**
* The default typeclass to write a `java.time.OffsetDateTime`,
* using '2011-12-03T10:15:30+02:00' format.
*/
implicit val DefaultOffsetDateTimeWrites =
temporalWrites[OffsetDateTime, DateTimeFormatter](
DateTimeFormatter.ISO_OFFSET_DATE_TIME)
/**
* The default typeclass to write a `java.time.ZonedDateTime`,
* using '2011-12-03T10:15:30+01:00[Europe/Paris]' format.
*/
implicit val DefaultZonedDateTimeWrites =
temporalWrites[ZonedDateTime, DateTimeFormatter](
DateTimeFormatter.ISO_ZONED_DATE_TIME)
/**
* The default typeclass to write a `java.time.LocalDate`,
* using '2011-12-03' format.
*/
implicit val DefaultLocalDateWrites =
temporalWrites[LocalDate, DateTimeFormatter](
DateTimeFormatter.ISO_LOCAL_DATE)
/**
* The default typeclass to write a `java.time.Instant`,
* using '2011-12-03T10:15:30Z' format.
*/
implicit val DefaultInstantWrites = new Writes[Instant] {
def writes(i: Instant): JsValue = JsString(i.toString)
}
/**
* Serializer for `java.time.LocalDateTime` as JSON number.
*
* {{{
* import java.time.LocalDateTime
* import play.api.libs.json.Writes
*
* implicit val ldtnWrites = Writes.LocalDateTimeNumberWrites
* }}}
*/
val LocalDateTimeNumberWrites: Writes[LocalDateTime] =
new Writes[LocalDateTime] {
def writes(t: LocalDateTime): JsValue = JsNumber(BigDecimal.valueOf(
t.toInstant(ZoneOffset.UTC).toEpochMilli))
}
/**
* Serializer for `java.time.ZonedDateTime` as JSON number.
*
* {{{
* import java.time.ZonedDateTime
* import play.api.libs.json.Writes
*
* implicit val zdtnWrites = Writes.ZonedDateTimeNumberWrites
* }}}
*/
val ZonedDateTimeNumberWrites: Writes[ZonedDateTime] =
new Writes[ZonedDateTime] {
def writes(t: ZonedDateTime): JsValue =
JsNumber(BigDecimal valueOf t.toInstant.toEpochMilli)
}
/**
* Serializer for `java.time.LocalDate` as JSON number.
*
* {{{
* import java.time.LocalDate
* import play.api.libs.json.Writes
*
* implicit val ldnWrites = Writes.LocalDateNumberWrites
* }}}
*/
val LocalDateNumberWrites: Writes[LocalDate] = new Writes[LocalDate] {
def writes(t: LocalDate): JsValue = JsNumber(BigDecimal.valueOf(
t.atStartOfDay.toInstant(ZoneOffset.UTC).toEpochMilli))
}
/**
* Serializer for `java.time.Instant` as JSON number.
*
* {{{
* import java.time.Instant
* import play.api.libs.json.Writes
*
* implicit val inWrites = Writes.InstantNumberWrites
* }}}
*/
val InstantNumberWrites: Writes[Instant] = new Writes[Instant] {
def writes(t: Instant): JsValue =
JsNumber(BigDecimal valueOf t.toEpochMilli)
}
/**
* Serializer for java.sql.Date
* @param pattern the pattern used by SimpleDateFormat
*/
def sqlDateWrites(pattern: String): Writes[java.sql.Date] = new Writes[java.sql.Date] {
def writes(d: java.sql.Date): JsValue = JsString(new java.text.SimpleDateFormat(pattern).format(d))
}
/**
* Serializer for java.util.UUID
*/
implicit object UuidWrites extends Writes[java.util.UUID] {
def writes(u: java.util.UUID) = JsString(u.toString)
}
/**
* Serializer for scala.Enumeration by name.
*/
implicit def enumNameWrites[E <: Enumeration]: Writes[E#Value] = new Writes[E#Value] {
def writes(value: E#Value): JsValue = JsString(value.toString)
}
/**
* Serializer for Any, used for the args of ValidationErrors.
*/
private[json] object anyWrites extends Writes[Any] {
def writes(a: Any): JsValue = a match {
case s: String => JsString(s)
case nb: Int => JsNumber(nb)
case nb: Short => JsNumber(nb)
case nb: Long => JsNumber(nb)
case nb: Double => JsNumber(nb)
case nb: Float => JsNumber(nb)
case b: Boolean => JsBoolean(b)
case js: JsValue => js
case x => JsString(x.toString)
}
}
}