-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
JsValue.scala
498 lines (395 loc) · 15.5 KB
/
JsValue.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package play.api.libs.json
import org.codehaus.jackson.{ JsonGenerator, JsonToken, JsonParser }
import org.codehaus.jackson.`type`.JavaType
import org.codehaus.jackson.map._
import org.codehaus.jackson.map.annotate.JsonCachable
import org.codehaus.jackson.map.`type`.{ TypeFactory, ArrayType }
import scala.collection._
import scala.collection.immutable.Stack
import scala.annotation.tailrec
import play.api.data.validation.ValidationError
import java.io.ByteArrayInputStream
case class JsResultException(errors: Seq[(JsPath, Seq[ValidationError])]) extends RuntimeException( "JsResultException(errors:%s)".format(errors) )
/**
* Generic json value
*/
sealed trait JsValue {
import scala.util.control.Exception._
/**
* Return the property corresponding to the fieldName, supposing we have a JsObject.
*
* @param fieldName the name of the property to lookup
* @return the resulting JsValue. If the current node is not a JsObject or doesn't have the property, a JsUndefined will be returned.
*/
def \(fieldName: String): JsValue = JsUndefined("'" + fieldName + "'" + " is undefined on object: " + this)
/**
* Return the element at a given index, supposing we have a JsArray.
*
* @param idx the index to lookup
* @param the resulting JsValue. If the current node is not a JsArray or the index is out of bounds, a JsUndefined will be returned.
*/
def apply(idx: Int): JsValue = JsUndefined(this.toString + " is not an array")
/**
* Lookup for fieldName in the current object and all descendants.
*
* @return the list of matching nodes
*/
def \\(fieldName: String): Seq[JsValue] = Nil
/**
* Tries to convert the node into a T. An implicit Reads[T] must be defined.
* Any error is mapped to None
*
* @return Some[T] if it succeeds, None if it fails.
*/
def asOpt[T](implicit fjs: Reads[T]): Option[T] = fjs.reads(this).fold(
invalid = _ => None,
valid = v => Some(v)
).filter {
case JsUndefined(_) => false
case _ => true
}
/**
* Tries to convert the node into a T, throwing an exception if it can't. An implicit Reads[T] must be defined.
*/
def as[T](implicit fjs: Reads[T]): T = fjs.reads(this).fold(
valid = identity,
invalid = e => throw new JsResultException(e)
)
/**
* Tries to convert the node into a JsResult[T] (Success or Error). An implicit Reads[T] must be defined.
*/
def validate[T](implicit rds: Reads[T]): JsResult[T] = rds.reads(this)
/**
* Transforms a JsValue into another JsValue using provided Json transformer Reads[JsValue]
*/
def transform[A <: JsValue](rds: Reads[A]): JsResult[A] = rds.reads(this)
override def toString = Json.stringify(this)
/**
* Prune the Json AST according to the provided JsPath
*/
//def prune(path: JsPath): JsValue = path.prune(this)
}
/**
* Represent a Json null value.
* with Scala 2.10-M7, this code generates WARNING : https://issues.scala-lang.org/browse/SI-6513
*/
case object JsNull extends JsValue
/**
* Represent a missing Json value.
*/
case class JsUndefined(error: String) extends JsValue
/**
* Represent a Json boolean value.
*/
case class JsBoolean(value: Boolean) extends JsValue
/**
* Represent a Json number value.
*/
case class JsNumber(value: BigDecimal) extends JsValue
/**
* Represent a Json string value.
*/
case class JsString(value: String) extends JsValue
/**
* Represent a Json array value.
*/
case class JsArray(value: Seq[JsValue] = List()) extends JsValue{
/**
* Access a value of this array.
*
* @param index Element index.
*/
override def apply(index: Int): JsValue = {
value.lift(index).getOrElse(JsUndefined("Array index out of bounds in " + this))
}
/**
* Lookup for fieldName in the current object and all descendants.
*
* @return the list of matching nodes
*/
override def \\(fieldName: String): Seq[JsValue] = value.flatMap(_ \\ fieldName)
/**
* Concatenates this array with the elements of an other array.
*/
def ++(other: JsArray): JsArray =
JsArray(value ++ other.value)
/**
* Append an element to this array.
*/
def :+(el: JsValue): JsArray = JsArray(value :+ el)
def append(el: JsValue): JsArray = this.:+(el)
/**
* Prepend an element to this array.
*/
def +:(el: JsValue): JsArray = JsArray(el +: value)
def prepend(el: JsValue): JsArray = this.+:(el)
}
/**
* Represent a Json object value.
*/
case class JsObject(fields: Seq[(String, JsValue)]) extends JsValue {
lazy val value: Map[String, JsValue] = fields.toMap
/**
* Return the property corresponding to the fieldName, supposing we have a JsObject.
*
* @param fieldName the name of the property to lookup
* @return the resulting JsValue. If the current node is not a JsObject or doesn't have the property, a JsUndefined will be returned.
*/
override def \(fieldName: String): JsValue = value.get(fieldName).getOrElse(super.\(fieldName))
/**
* Lookup for fieldName in the current object and all descendants.
*
* @return the list of matching nodes
*/
override def \\(fieldName: String): Seq[JsValue] = {
value.foldLeft(Seq[JsValue]())((o, pair) => pair match {
case (key, value) if key == fieldName => o ++ (value +: (value \\ fieldName))
case (_, value) => o ++ (value \\ fieldName)
})
}
/**
* Return all keys
*/
def keys: Set[String] = fields.map(_._1).toSet
/**
* Return all values
*/
def values: Set[JsValue] = fields.map(_._2).toSet
def fieldSet: Set[(String, JsValue)] = fields.toSet
/**
* Merge this object with an other one. Values from other override value of the current object.
*/
def ++(other: JsObject): JsObject =
JsObject(fields.filterNot(field => other.keys(field._1)) ++ other.fields)
/**
* removes one field from JsObject
*/
def -(otherField: String): JsObject =
JsObject(fields.filterNot( _._1 == otherField ))
/**
* adds one field from JsObject
*/
def +(otherField: (String, JsValue)): JsObject =
JsObject(fields :+ otherField)
/**
* merges everything in depth and doesn't stop at first level as ++
* TODO : improve because coding is nasty there
*/
def deepMerge(other: JsObject): JsObject = {
def step(fields: List[(String, JsValue)], others: List[(String, JsValue)]): Seq[(String, JsValue)] = {
others match {
case List() => fields
case List(sv) =>
var found = false
val newFields = fields match {
case List() => List(sv)
case _ => fields.foldLeft(List[(String, JsValue)]()){ (acc, field) => field match {
case (key, obj: JsObject) if(key == sv._1) =>
found = true
acc :+ key -> {
sv._2 match {
case o @ JsObject(_) => obj.deepMerge(o)
case js => js
}
}
case (key, value) if(key == sv._1) =>
found = true
acc :+ key -> sv._2
case (key, value) => acc :+ key -> value
} }
}
if(!found) fields :+ sv
else newFields
case head :: tail =>
var found = false
val headFields = fields match {
case List() => List(head)
case _ => fields.foldLeft(List[(String, JsValue)]()){ (acc, field) => field match {
case (key, obj: JsObject) if(key == head._1) =>
found = true
acc :+ key -> {
head._2 match {
case o @ JsObject(_) => obj.deepMerge(o)
case js => js
}
}
case (key, value) if(key == head._1) =>
found = true
acc :+ key -> head._2
case (key, value) => acc :+ key -> value
} }
}
if(!found) step(fields :+ head, tail)
else step(headFields, tail)
}
}
JsObject(step(fields.toList, other.fields.toList))
}
override def equals(other: Any): Boolean =
other match {
case that: JsObject =>
(that canEqual this) &&
fieldSet == that.fieldSet
case _ => false
}
def canEqual(other: Any): Boolean = other.isInstanceOf[JsObject]
override def hashCode: Int = fieldSet.hashCode()
}
// -- Serializers.
@JsonCachable
private[json] class JsValueSerializer extends JsonSerializer[JsValue] {
def serialize(value: JsValue, json: JsonGenerator, provider: SerializerProvider) {
value match {
case JsNumber(v) => json.writeNumber(v.bigDecimal)
case JsString(v) => json.writeString(v)
case JsBoolean(v) => json.writeBoolean(v)
case JsArray(elements) => {
json.writeStartArray()
elements.foreach { t =>
serialize(t, json, provider)
}
json.writeEndArray()
}
case JsObject(values) => {
json.writeStartObject()
values.foreach { t =>
json.writeFieldName(t._1)
serialize(t._2, json, provider)
}
json.writeEndObject()
}
case JsNull => json.writeNull()
case JsUndefined(error) => {
json.writeNull()
}
}
}
}
private[json] sealed trait DeserializerContext {
def addValue(value: JsValue): DeserializerContext
}
private[json] case class ReadingList(content: List[JsValue]) extends DeserializerContext {
override def addValue(value: JsValue): DeserializerContext = {
ReadingList(content :+ value)
}
}
// Context for reading an Object
private[json] case class KeyRead(content: List[(String, JsValue)], fieldName: String) extends DeserializerContext {
def addValue(value: JsValue): DeserializerContext = ReadingMap(content :+ (fieldName -> value))
}
// Context for reading one item of an Object (we already red fieldName)
private[json] case class ReadingMap(content: List[(String, JsValue)]) extends DeserializerContext {
def setField(fieldName: String) = KeyRead(content, fieldName)
def addValue(value: JsValue): DeserializerContext = throw new Exception("Cannot add a value on an object without a key, malformed JSON object!")
}
@JsonCachable
private[json] class JsValueDeserializer(factory: TypeFactory, klass: Class[_]) extends JsonDeserializer[Object] {
def deserialize(jp: JsonParser, ctxt: DeserializationContext): JsValue = {
val value = deserialize(jp, ctxt, List())
if (!klass.isAssignableFrom(value.getClass)) {
throw ctxt.mappingException(klass)
}
value
}
@tailrec
final def deserialize(jp: JsonParser, ctxt: DeserializationContext, parserContext: List[DeserializerContext]): JsValue = {
if (jp.getCurrentToken == null) {
jp.nextToken()
}
val (maybeValue, nextContext) = (jp.getCurrentToken, parserContext) match {
case (JsonToken.VALUE_NUMBER_INT, c) => (Some(JsNumber(jp.getLongValue)), c)
case (JsonToken.VALUE_NUMBER_FLOAT, c) => (Some(JsNumber(jp.getDoubleValue)), c)
case (JsonToken.VALUE_STRING, c) => (Some(JsString(jp.getText)), c)
case (JsonToken.VALUE_TRUE, c) => (Some(JsBoolean(true)), c)
case (JsonToken.VALUE_FALSE, c) => (Some(JsBoolean(false)), c)
case (JsonToken.VALUE_NULL, c) => (Some(JsNull), c)
case (JsonToken.START_ARRAY, c) => (None, (ReadingList(List())) +: c)
case (JsonToken.END_ARRAY, ReadingList(content) :: stack) => (Some(JsArray(content)), stack)
case (JsonToken.END_ARRAY, _) => throw new RuntimeException("We should have been reading list, something got wrong")
case (JsonToken.START_OBJECT, c) => (None, ReadingMap(List()) +: c)
case (JsonToken.FIELD_NAME, (c: ReadingMap) :: stack) => (None, c.setField(jp.getCurrentName) +: stack)
case (JsonToken.FIELD_NAME, _) => throw new RuntimeException("We should be reading map, something got wrong")
case (JsonToken.END_OBJECT, ReadingMap(content) :: stack) => (Some(JsObject(content)), stack)
case (JsonToken.END_OBJECT, _) => throw new RuntimeException("We should have been reading an object, something got wrong")
case (JsonToken.NOT_AVAILABLE, _) => throw new RuntimeException("We should have been reading an object, something got wrong")
case (JsonToken.VALUE_EMBEDDED_OBJECT, _) => throw new RuntimeException("We should have been reading an object, something got wrong")
}
// Read ahead
jp.nextToken()
maybeValue match {
case Some(v) if nextContext.isEmpty && jp.getCurrentToken == null =>
//done, no more tokens and got a value!
v
case Some(v) if nextContext.isEmpty =>
//strange, got value, but there is more tokens and have no prior context!
throw new Exception("Malformed JSON: Got a sequence of JsValue outside an array or an object.")
case maybeValue =>
val toPass = maybeValue.map { v =>
val previous :: stack = nextContext
(previous.addValue(v)) +: stack
}.getOrElse(nextContext)
deserialize(jp, ctxt, toPass)
}
}
}
private[json] class PlayDeserializers(classLoader: ClassLoader) extends Deserializers.Base {
override def findBeanDeserializer(javaType: JavaType, config: DeserializationConfig,
provider: DeserializerProvider, beanDesc: BeanDescription,
property: BeanProperty) = {
val klass = javaType.getRawClass
if (classOf[JsValue].isAssignableFrom(klass) || klass == JsNull.getClass) {
new JsValueDeserializer(config.getTypeFactory, klass)
} else null
}
}
private[json] class PlaySerializers extends Serializers.Base {
override def findSerializer(config: SerializationConfig, javaType: JavaType, beanDesc: BeanDescription, beanProp: BeanProperty) = {
val ser: Object = if (classOf[JsValue].isAssignableFrom(beanDesc.getBeanClass)) {
new JsValueSerializer
} else {
null
}
ser.asInstanceOf[JsonSerializer[Object]]
}
}
private[json] object JacksonJson{
import org.codehaus.jackson.Version
import org.codehaus.jackson.map.module.SimpleModule
import org.codehaus.jackson.map.Module.SetupContext
private[this] val classLoader = Thread.currentThread().getContextClassLoader
private[this] val mapper = new ObjectMapper
object module extends SimpleModule("PlayJson", Version.unknownVersion()) {
override def setupModule(context: SetupContext) {
context.addDeserializers(new PlayDeserializers(classLoader))
context.addSerializers(new PlaySerializers)
}
}
mapper.registerModule(module)
private[this] lazy val jsonFactory = new org.codehaus.jackson.JsonFactory(mapper)
private[this] def stringJsonGenerator(out: java.io.StringWriter) = jsonFactory.createJsonGenerator(out)
private[this] def jsonParser(c: String): JsonParser = jsonFactory.createJsonParser(c)
// Pass in ByteArrayInputStream to work around https://github.com/FasterXML/jackson-core/issues/42
private[this] def jsonParser(data: Array[Byte]): JsonParser = jsonFactory.createJsonParser(new ByteArrayInputStream(data))
def parseJsValue(data: Array[Byte]): JsValue = {
mapper.readValue(jsonParser(data), classOf[JsValue])
}
def parseJsValue(input: String): JsValue = {
mapper.readValue(jsonParser(input), classOf[JsValue])
}
def generateFromJsValue(jsValue: JsValue): String = {
val sw = new java.io.StringWriter
val gen = stringJsonGenerator(sw)
mapper.writeValue(gen, jsValue)
sw.flush
sw.getBuffer.toString
}
def prettyPrint(jsValue: JsValue): String = {
val sw = new java.io.StringWriter
val gen = stringJsonGenerator(sw).setPrettyPrinter(
new org.codehaus.jackson.util.DefaultPrettyPrinter()
)
mapper.writerWithDefaultPrettyPrinter().writeValue(gen, jsValue)
sw.flush
sw.getBuffer.toString
}
}