blob: 378d67d66544e8136c52f3938cfdeba487c29110 (
plain) (
blame)
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
|
package cc.spray.json
package formats
/*
* Original implementation (C) 2009-2011 Debasish Ghosh
* Adapted and extended in 2011 by Mathias Doenitz
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
trait BasicFormats {
implicit object IntJsonFormat extends JsonFormat[Int] {
def write(x: Int) = JsNumber(x)
def read(value: JsValue) = value match {
case JsNumber(x) => x.intValue
case _ => throw new DeserializationException("Int expected")
}
}
implicit object LongJsonFormat extends JsonFormat[Long] {
def write(x: Long) = JsNumber(x)
def read(value: JsValue) = value match {
case JsNumber(x) => x.longValue
case _ => throw new DeserializationException("Long expected")
}
}
implicit object FloatJsonFormat extends JsonFormat[Float] {
def write(x: Float) = JsNumber(x)
def read(value: JsValue) = value match {
case JsNumber(x) => x.floatValue
case _ => throw new DeserializationException("Float expected")
}
}
implicit object DoubleJsonFormat extends JsonFormat[Double] {
def write(x: Double) = JsNumber(x)
def read(value: JsValue) = value match {
case JsNumber(x) => x.doubleValue
case _ => throw new DeserializationException("Double expected")
}
}
implicit object ByteJsonFormat extends JsonFormat[Byte] {
def write(x: Byte) = JsNumber(x)
def read(value: JsValue) = value match {
case JsNumber(x) => x.byteValue
case _ => throw new DeserializationException("Byte expected")
}
}
implicit object ShortJsonFormat extends JsonFormat[Short] {
def write(x: Short) = JsNumber(x)
def read(value: JsValue) = value match {
case JsNumber(x) => x.shortValue
case _ => throw new DeserializationException("Short expected")
}
}
implicit object BigDecimalJsonFormat extends JsonFormat[BigDecimal] {
def write(x: BigDecimal) = JsNumber(x)
def read(value: JsValue) = value match {
case JsNumber(x) => x
case _ => throw new DeserializationException("String expected")
}
}
implicit object BigIntJsonFormat extends JsonFormat[BigInt] {
def write(x: BigInt) = JsNumber(x)
def read(value: JsValue) = value match {
case JsNumber(x) => x.toBigInt
case _ => throw new DeserializationException("BigInt expected")
}
}
implicit object UnitJsonFormat extends JsonFormat[Unit] {
def write(x: Unit) = JsNumber(1)
def read(value: JsValue) {}
}
implicit object BooleanJsonFormat extends JsonFormat[Boolean] {
def write(x: Boolean) = JsBoolean(x)
def read(value: JsValue) = value match {
case JsTrue => true
case JsFalse => false
case _ => throw new DeserializationException("Boolean expected")
}
}
implicit object CharJsonFormat extends JsonFormat[Char] {
def write(x: Char) = JsString(String.valueOf(x))
def read(value: JsValue) = value match {
case JsString(x) if x.length == 1 => x.charAt(0)
case _ => throw new DeserializationException("Char expected")
}
}
implicit object StringJsonFormat extends JsonFormat[String] {
def write(x: String) = JsString(x)
def read(value: JsValue) = value match {
case JsString(x) => x
case _ => throw new DeserializationException("String expected")
}
}
implicit object SymbolJsonFormat extends JsonFormat[Symbol] {
def write(x: Symbol) = JsString(x.name)
def read(value: JsValue) = value match {
case JsString(x) => Symbol(x)
case _ => throw new DeserializationException("Symbol expected")
}
}
}
|