/
ExtractableJsonAstNode.scala
110 lines (103 loc) · 3.57 KB
/
ExtractableJsonAstNode.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
package org.json4s
class ExtractableJsonAstNode(jv: JValue) {
/**
* Extract a value from a JSON.
* <p>
* Value can be:
* <ul>
* <li>case class</li>
* <li>primitive (String, Boolean, Date, etc.)</li>
* <li>supported collection type (List, Seq, Map[String, _], Set)</li>
* <li>any type which has a configured custom deserializer</li>
* </ul>
* <p>
* Example:<pre>
* case class Person(name: String)
* JObject(JField("name", JString("joe")) :: Nil).extract[Person] == Person("joe")
* </pre>
*/
def extract[A](implicit formats: Formats, mf: scala.reflect.Manifest[A]): A =
Extraction.extract(jv)(formats, mf)
/**
* Extract a value from a JSON.
* <p>
* Value can be:
* <ul>
* <li>case class</li>
* <li>primitive (String, Boolean, Date, etc.)</li>
* <li>supported collection type (List, Seq, Map[String, _], Set)</li>
* <li>any type which has a configured custom deserializer</li>
* </ul>
* <p>
* Example:<pre>
* case class Person(name: String)
* JObject(JField("name", JString("joe")) :: Nil).extractOpt[Person] == Some(Person("joe"))
* </pre>
*/
def extractOpt[A](implicit formats: Formats, mf: scala.reflect.Manifest[A]): Option[A] =
Extraction.extractOpt(jv)(formats, mf)
/**
* Extract a value from a JSON using a default value.
* <p>
* Value can be:
* <ul>
* <li>case class</li>
* <li>primitive (String, Boolean, Date, etc.)</li>
* <li>supported collection type (List, Seq, Map[String, _], Set)</li>
* <li>any type which has a configured custom deserializer</li>
* </ul>
* <p>
* Example:<pre>
* case class Person(name: String)
* JNothing.extractOrElse(Person("joe")) == Person("joe")
* </pre>
*/
def extractOrElse[A](default: ⇒ A)(implicit formats: Formats, mf: scala.reflect.Manifest[A]): A =
Extraction.extractOpt(jv)(formats, mf).getOrElse(default)
/**
* Given that an implicit reader of type `A` is in scope
* It will deserialize the org.json4s.JsonAST.JValue to an object of type `A`
*
* Example:
* {{{
* case class Person(name: String)
* implicit object PersonReader extends Reader[Person] {
* def read(json: JValue): Person = Person((json \ "name").extract[String])
* }
* JObject(JField("name", JString("Joe")) :: Nil).as[Person]
* }}}
*/
def as[A](implicit reader: Reader[A]): A = reader.read(jv)
/**
* Given that an implicit reader of type `A` is in scope
* It will deserialize the org.json4s.JsonAST.JValue to an object of type Option[`A`]
*
* Example:
* {{{
* case class Person(name: String)
* implicit object PersonReader extends Reader[Person] {
* def read(json: JValue): Person = Person((json \ "name").extract[String])
* }
* JObject(JField("name", JString("Joe")) :: Nil).getAs[Person]
* }}}
*/
def getAs[A](implicit reader: Reader[A]): Option[A] = try {
Option(reader.read(jv))
} catch { case _: Throwable ⇒ None }
/**
* Given that an implicit reader of type `A` is in scope
* It will deserialize the org.json4s.JsonAST.JValue to an object of type `A`
* if an error occurs it will return the default value.
*
* Example:
* {{{
* case class Person(name: String)
* implicit object PersonReader extends Reader[Person] {
* def read(json: JValue): Person = Person((json \ "name").extract[String])
* }
* JObject(JField("name", JString("Joe")) :: Nil).getAsOrElse(Person("Tom"))
* }}}
*/
def getAsOrElse[A](default: ⇒ A)(implicit reader: Reader[A]): A =
getAs(reader) getOrElse default
}