-
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathJValueConverters.scala
More file actions
130 lines (103 loc) · 3.38 KB
/
Copy pathJValueConverters.scala
File metadata and controls
130 lines (103 loc) · 3.38 KB
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
package play.api.libs.json.json4s
import scala.language.implicitConversions
import org.json4s.{
JArray,
JBool,
JDecimal,
JDouble,
JField,
JInt,
JLong,
JNothing,
JNull,
JObject,
JSet,
JString,
JValue
}
import play.api.libs.json.{
JsArray,
JsBoolean,
JsFalse,
JsNumber,
JsNull,
JsObject,
JsString,
JsTrue,
JsValue
}
/**
* {{{
* import play.api.libs.json.json4s.JValueConverters
*
* val jsTrue: JsBoolean = JBool.True
* val jbool: JBool = JsTrue
* }}}
*/
object JValueConverters extends LowPriorityJValueImplicits {
implicit val jnull2JsNull: JNull.type => JsNull.type = _ => JsNull
implicit val jsnull2JNull: JsNull.type => JNull.type = _ => JNull
implicit val jnothing2JsNull: JNothing.type => JsNull.type = _ => JsNull
implicit def jbool2JsBoolean(jb: JBool): JsBoolean = jb match {
case JBool.True => JsTrue
case _ => JsFalse
}
implicit val jstrue2JBool: JsTrue.type => JBool = _ => JBool.True
implicit val jsfalse2JBool: JsFalse.type => JBool = _ => JBool.False
implicit def jsbool2JBool(jb: JsBoolean): JBool = jb match {
case JsTrue => JBool.True
case _ => JBool.False
}
implicit def jstring2JsString(js: JString): JsString = JsString(js.s)
implicit def jsstring2Jtring(js: JsString): JString = JString(js.value)
implicit def jdecimal2JsNumber(jd: JDecimal): JsNumber = JsNumber(jd.num)
implicit def jsnumber2JDecimal(jn: JsNumber): JDecimal = JDecimal(jn.value)
implicit def jint2JsNumber(ji: JInt): JsNumber = JsNumber(BigDecimal(ji.num))
implicit def jdouble2JsNumber(ji: JDouble): JsNumber =
JsNumber(BigDecimal(ji.num))
implicit def jlong2JsNumber(ji: JLong): JsNumber =
JsNumber(BigDecimal(ji.num))
implicit def jfield2Field(jf: JField): (String, JsValue) =
jf._1 -> jvalue2JsValue(jf._2)
implicit def field2JField(jf: (String, JsValue)): JField =
jf._1 -> jsvalue2JValue(jf._2)
implicit def jobject2JsObject(jo: JObject): JsObject =
JsObject(jo.obj.map({
case (nme, jv) => nme -> jvalue2JsValue(jv)
})(scala.collection.breakOut))
implicit def jsobject2JObject(jo: JsObject): JObject =
JObject(jo.fields.toList.map(field2JField))
implicit def jarray2JsArray(ja: JArray): JsArray =
JsArray(ja.arr.map(jvalue2JsValue))
implicit def jsarray2JArray(ja: JsArray): JArray =
JArray(ja.value.toList.map(jsvalue2JValue))
implicit def jset2JsArray(js: JSet): JsArray =
JsArray(js.set.toList.map(jvalue2JsValue))
}
private[json4s] sealed trait LowPriorityJValueImplicits {
_: JValueConverters.type =>
implicit def jvalue2JsValue(jv: JValue): JsValue = jv match {
case a @ JArray(_) => jarray2JsArray(a)
case JDouble(d) => JsNumber(BigDecimal(d))
case JInt(i) => JsNumber(BigDecimal(i))
case JLong(l) => JsNumber(BigDecimal(l))
case JNull => JsNull
case JNothing => JsNull
case JBool.True => JsTrue
case JBool.False => JsFalse
case JBool(b) => JsBoolean(b)
case s @ JSet(_) => jset2JsArray(s)
case JString(s) => JsString(s)
case JDecimal(n) => JsNumber(n)
case o @ JObject(fs) => jobject2JsObject(o)
}
implicit def jsvalue2JValue(jv: JsValue): JValue = jv match {
case a @ JsArray(_) => jsarray2JArray(a)
case JsNull => JNull
case JsTrue => JBool.True
case JsFalse => JBool.False
case JsString(s) => JString(s)
case JsNumber(n) => JDecimal(n)
case o @ JsObject(_) => jsobject2JObject(o)
}
}