-
Notifications
You must be signed in to change notification settings - Fork 112
/
DecodeJson.scala
380 lines (321 loc) · 11.1 KB
/
DecodeJson.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
package argonaut
import scala.collection.generic.CanBuildFrom
import scala.util.control.Exception.catching
import Json._
trait DecodeJson[A] {
/**
* Decode the given hcursor. Alias for `decode`.
*/
def apply(c: HCursor): DecodeResult[A] = decode(c)
/**
* Decode the given hcursor.
*/
def decode(c: HCursor): DecodeResult[A]
/**
* Decode the given acursor.
*/
def tryDecode(c: ACursor): DecodeResult[A] = c.either match {
case Left(invalid) => DecodeResult.fail("Attempt to decode value on failed cursor.", invalid.history)
case Right(valid) => decode(valid)
}
/**
* Decode the given json.
*/
def decodeJson(j: Json): DecodeResult[A] = decode(j.hcursor)
/**
* Transform the incoming HCursor to produce another DecodeJson instance.
*/
def flatMapCursor(f: HCursor => DecodeResult[HCursor]): DecodeJson[A] = {
val original = this
new DecodeJson[A]{
override def decode(c: HCursor): DecodeResult[A] = for {
fr <- f(c)
result <- original.decode(fr)
} yield result
}
}
/**
* Covariant functor.
*/
def map[B](f: A => B): DecodeJson[B] = {
def thisDecode = decode(_)
def thisTryDecode = tryDecode(_)
new DecodeJson[B] {
override def decode(c: HCursor): DecodeResult[B] = {
thisDecode(c).map(f)
}
override def tryDecode(c: ACursor): DecodeResult[B] = {
thisTryDecode(c).map(f)
}
}
}
/**
* Monad.
*/
def flatMap[B](f: A => DecodeJson[B]): DecodeJson[B] = {
def thisDecode = decode(_)
def thisTryDecode = tryDecode(_)
new DecodeJson[B] {
override def decode(c: HCursor): DecodeResult[B] = {
thisDecode(c).flatMap(a => f(a).decode(c))
}
override def tryDecode(c: ACursor): DecodeResult[B] = {
thisTryDecode(c).flatMap(a => f(a).tryDecode(c))
}
}
}
/**
* Build a new DecodeJson codec with the specified name.
*/
def setName(n: String): DecodeJson[A] = {
DecodeJson[A](c => apply(c).result.fold(
{ case (_, h) => DecodeResult.fail(n, h) },
a => DecodeResult.ok(a)
))
}
/**
* Build a new DecodeJson codec with the specified precondition that f(c) == true.
*/
def validate(f: HCursor => Boolean, message: => String) = {
DecodeJson(c => if (f(c)) apply(c) else DecodeResult.fail[A](message, c.history))
}
/**
* Build a new DecodeJson codec with the precondition that the cursor focus is object with exactly n field.
*/
def validateFields(n: Int) = {
validate(_.focus.obj exists (_.size == n), "Expected json object with exactly [" + n + "] fields.")
}
/**
* Combine two decoders.
*/
def &&&[B](x: DecodeJson[B]): DecodeJson[(A, B)] = {
DecodeJson(j => for {
a <- this(j)
b <- x(j)
} yield (a, b))
}
/**
* Choose the first succeeding decoder.
*/
def |||[B, AA](x: => DecodeJson[B])(implicit evA: A <:< AA, evB: B <:< AA): DecodeJson[AA] = {
DecodeJson[AA](c => {
val q = apply(c).map(evA)
q.result.fold(_ => x(c).map(evB), _ => q)
})
}
/**
* Run one or another decoder.
*/
def split[B](x: DecodeJson[B]): Either[HCursor, HCursor] => DecodeResult[Either[A, B]] = {
c => c.fold(a => this(a).map(Left.apply), a => x(a).map(Right.apply))
}
/**
* Run two decoders.
*/
def product[B](x: DecodeJson[B]): (HCursor, HCursor) => DecodeResult[(A, B)] = {
case (a1, a2) => for {
a <- this(a1)
b <- x(a2)
} yield (a, b)
}
/**
* Widen A into AA.
*/
def widen[AA]()(implicit ev: A <:< AA) = map(ev)
}
object DecodeJson extends DecodeJsons {
def apply[A](r: HCursor => DecodeResult[A]): DecodeJson[A] = {
new DecodeJson[A] {
def decode(c: HCursor) = r(c)
}
}
def withReattempt[A](r: ACursor => DecodeResult[A]): DecodeJson[A] = {
new DecodeJson[A] {
def decode(c: HCursor): DecodeResult[A] = tryDecode(c.acursor)
override def tryDecode(c: ACursor) = r(c)
}
}
def derive[A]: DecodeJson[A] = macro internal.Macros.materializeDecodeImpl[A]
def of[A: DecodeJson] = implicitly[DecodeJson[A]]
}
trait DecodeJsons extends GeneratedDecodeJsons {
def optionDecoder[A](k: Json => Option[A], e: String): DecodeJson[A] = {
DecodeJson[A](a => k(a.focus) match {
case None => DecodeResult.fail(e, a.history)
case Some(w) => DecodeResult.ok(w)
})
}
/**
* Construct a succeeding decoder from the given function.
*/
def decodeArr[A](f: HCursor => A): DecodeJson[A] = DecodeJson(j => DecodeResult.ok(f(j)))
def tryTo[A](f: => A): Option[A] = catching(classOf[IllegalArgumentException]).opt(f)
implicit def HCursorDecodeJson: DecodeJson[HCursor] = decodeArr(q => q)
implicit def JsonDecodeJson: DecodeJson[Json] = decodeArr(j => j.focus)
def CanBuildFromDecodeJson[A, C[_]](implicit e: DecodeJson[A], c: CanBuildFrom[Nothing, A, C[A]]): DecodeJson[C[A]] = {
DecodeJson(a =>
a.downArray.hcursor match {
case None => {
if (a.focus.isArray) {
DecodeResult.ok(c.apply.result)
} else {
DecodeResult.fail("[A]", a.history)
}
}
case Some(hcursor) => {
hcursor.rights match {
case Some(elements) => {
(hcursor.focus :: elements).foldLeft(DecodeResult.ok(c.apply)){(working, elem) =>
for {
w <- working
e <- elem.jdecode[A]
} yield w += e
}.map(_.result)
}
case _ => {
DecodeResult.fail("[A]", a.history)
}
}
}
}
)
}
implicit val UUIDDecodeJson: DecodeJson[java.util.UUID] = {
optionDecoder(_.string.flatMap(s =>
tryTo(java.util.UUID.fromString(s))), "UUID")
}
implicit def ListDecodeJson[A](implicit e: DecodeJson[A]): DecodeJson[List[A]] = CanBuildFromDecodeJson[A, List]
implicit def VectorDecodeJson[A](implicit e: DecodeJson[A]): DecodeJson[Vector[A]] = CanBuildFromDecodeJson[A, Vector]
implicit def StreamDecodeJson[A](implicit e: DecodeJson[A]): DecodeJson[Stream[A]] = CanBuildFromDecodeJson[A, Stream]
implicit def SetDecodeJson[A](implicit e: DecodeJson[A]): DecodeJson[Set[A]] = CanBuildFromDecodeJson[A, Set]
implicit def UnitDecodeJson: DecodeJson[Unit] = {
DecodeJson.apply[Unit](a => if (a.focus.isNull || a.focus == jEmptyObject || a.focus == jEmptyArray) {
DecodeResult.ok(())
} else {
DecodeResult.fail("Unit", a.history)
})
}
implicit def StringDecodeJson: DecodeJson[String] = optionDecoder(_.string, "String")
implicit def DoubleDecodeJson: DecodeJson[Double] = {
optionDecoder[Double](x => {
if (x.isNull) {
Some(Double.NaN)
} else {
x.number.flatMap(n => tryTo(n.truncateToDouble)).orElse(x.string.flatMap(s => tryTo(s.toDouble)))
}
}, "Double")
}
implicit def FloatDecodeJson: DecodeJson[Float] = {
optionDecoder[Float](x => {
if (x.isNull) {
Some(Float.NaN)
} else {
x.number.flatMap(n => tryTo(n.truncateToFloat)).orElse(x.string.flatMap(s => tryTo(s.toFloat)))
}
}, "Float")
}
implicit def IntDecodeJson: DecodeJson[Int] = {
optionDecoder(x =>
(x.number map (_.truncateToInt)).orElse(
(x.string flatMap (s => tryTo(s.toInt)))), "Int")
}
implicit def LongDecodeJson: DecodeJson[Long] = {
optionDecoder(x =>
(x.number map (_.truncateToLong)).orElse(
(x.string flatMap (s => tryTo(s.toLong)))), "Long")
}
implicit def ShortDecodeJson: DecodeJson[Short] = {
optionDecoder(x =>
(x.number map (_.truncateToShort)).orElse(
(x.string flatMap (s => tryTo(s.toShort)))), "Short")
}
implicit def ByteDecodeJson: DecodeJson[Byte] = {
optionDecoder(x =>
(x.number map (_.truncateToByte)).orElse(
(x.string flatMap (s => tryTo(s.toByte)))), "Byte")
}
implicit def BigIntDecodeJson: DecodeJson[BigInt] = {
optionDecoder(x =>
(x.number flatMap (_.truncateToBigInt)).orElse(
(x.string flatMap (s => tryTo(BigInt(s))))), "BigInt")
}
implicit def BigDecimalDecodeJson: DecodeJson[BigDecimal] = {
optionDecoder(x =>
(x.number map (_.toBigDecimal)).orElse(
(x.string flatMap (s => tryTo(BigDecimal(s))))), "BigDecimal")
}
implicit def BooleanDecodeJson: DecodeJson[Boolean] = {
optionDecoder(x => x.bool.orElse(x.string.flatMap(s => tryTo(s.toBoolean))), "Boolean")
}
implicit def CharDecodeJson: DecodeJson[Char] = {
optionDecoder(_.string.flatMap(s => if(s.length == 1) Some(s(0)) else None), "Char")
}
implicit def JDoubleDecodeJson: DecodeJson[java.lang.Double] = {
DoubleDecodeJson.map(d => d: java.lang.Double)
}
implicit def JFloatDecodeJson: DecodeJson[java.lang.Float] = {
FloatDecodeJson.map(f => f: java.lang.Float)
}
implicit def JIntegerDecodeJson: DecodeJson[java.lang.Integer] = {
optionDecoder(_.number.flatMap(s => tryTo(s.truncateToInt)), "java.lang.Integer")
}
implicit def JLongDecodeJson: DecodeJson[java.lang.Long] = {
optionDecoder(_.number.flatMap(s => tryTo(s.truncateToLong)), "java.lang.Long")
}
implicit def JShortDecodeJson: DecodeJson[java.lang.Short] = {
optionDecoder(_.number.flatMap(s => tryTo(s.truncateToShort)), "java.lang.Short")
}
implicit def JByteDecodeJson: DecodeJson[java.lang.Byte] = {
optionDecoder(_.number.flatMap(s => tryTo(s.truncateToByte)), "java.lang.Byte")
}
implicit def JBooleanDecodeJson: DecodeJson[java.lang.Boolean] = {
optionDecoder(_.bool.map(q => q), "java.lang.Boolean")
}
implicit def JCharacterDecodeJson: DecodeJson[java.lang.Character] = {
optionDecoder(_.string.flatMap(s => if(s.length == 1) Some(s(0)) else None), "java.lang.Character")
}
implicit def OptionDecodeJson[A](implicit e: DecodeJson[A]): DecodeJson[Option[A]] = {
DecodeJson.withReattempt[Option[A]](a => a.success match {
case None => DecodeResult.ok(None)
case Some(valid) => {
if (valid.focus.isNull) {
DecodeResult.ok(None)
} else {
e(valid).option
}
}
})
}
implicit def EitherDecodeJson[A, B](implicit ea: DecodeJson[A], eb: DecodeJson[B]): DecodeJson[Either[A, B]] = {
DecodeJson[Either[A, B]](a => {
val l = (a --\ "Left").success
val r = (a --\ "Right").success
(l, r) match {
case (Some(c), None) => ea(c) map (Left(_))
case (None, Some(c)) => eb(c) map (Right(_))
case _ => DecodeResult.fail("[A, B]Either[A, B]", a.history)
}
})
}
implicit def MapDecodeJson[V](implicit e: DecodeJson[V]): DecodeJson[Map[String, V]] = {
DecodeJson(a =>
a.fields match {
case None => DecodeResult.fail("[V]Map[String, V]", a.history)
case Some(s) => {
def spin(x: List[JsonField], m: DecodeResult[Map[String, V]]): DecodeResult[Map[String, V]] = {
x match {
case Nil => m
case h::t => {
spin(t, for {
mm <- m
v <- a.get(h)(e)
} yield mm + ((h, v)))
}
}
}
spin(s, DecodeResult.ok(Map.empty[String, V]))
}
}
)
}
}