forked from spray/spray-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicFormats.scala
95 lines (82 loc) · 3.88 KB
/
BasicFormats.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
/*
* Original implementation (C) 2009-2011 Debasish Ghosh
* Adapted and extended in 2011 by Mathias Doenitz
* Adapted to reming in 2015 by Jesse Kinkead
*
* 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.
*/
package reming
/** Provides the streaming formats for numbers, String, and Symbol. Note that the number readers are
* non-strict in that they will happily round floating-point numbers to Integers.
*/
trait BasicFormats {
implicit object IntFormat extends JsonFormat[Int] {
override def write(value: Int, printer: JsonPrinter): Unit = printer.printInt(value)
override def read(parser: JsonParser): Int = parser.readInt()
}
implicit object LongFormat extends JsonFormat[Long] {
override def write(value: Long, printer: JsonPrinter): Unit = printer.printLong(value)
override def read(parser: JsonParser): Long = parser.readLong()
}
implicit object FloatFormat extends JsonFormat[Float] {
override def write(value: Float, printer: JsonPrinter): Unit = printer.printFloat(value)
override def read(parser: JsonParser): Float = parser.readNumber().toFloat
}
implicit object DoubleFormat extends JsonFormat[Double] {
override def write(value: Double, printer: JsonPrinter): Unit = printer.printDouble(value)
override def read(parser: JsonParser): Double = parser.readNumber().toDouble
}
implicit object ByteFormat extends JsonFormat[Byte] {
override def write(value: Byte, printer: JsonPrinter): Unit = printer.printByte(value)
override def read(parser: JsonParser): Byte = parser.readNumber().toByte
}
implicit object ShortFormat extends JsonFormat[Short] {
override def write(value: Short, printer: JsonPrinter): Unit = printer.printShort(value)
override def read(parser: JsonParser): Short = parser.readNumber().toShort
}
implicit object BigDecimalFormat extends JsonFormat[BigDecimal] {
override def write(value: BigDecimal, printer: JsonPrinter): Unit = {
printer.printBigDecimal(value)
}
override def read(parser: JsonParser): BigDecimal = parser.readNumber()
}
implicit object BigIntFormat extends JsonFormat[BigInt] {
override def write(value: BigInt, printer: JsonPrinter): Unit = printer.printBigInt(value)
override def read(parser: JsonParser): BigInt = parser.readNumber().toBigInt
}
implicit object BooleanFormat extends JsonFormat[Boolean] {
override def write(value: Boolean, printer: JsonPrinter): Unit = {
printer.printBoolean(value)
}
override def read(parser: JsonParser): Boolean = parser.readBoolean()
}
implicit object CharFormat extends JsonFormat[Char] {
override def write(value: Char, printer: JsonPrinter): Unit = {
printer.printString(value.toString)
}
override def read(parser: JsonParser): Char = parser.readString() match {
case x if x.length == 1 => x.charAt(0)
case x => deserializationError("Expected Char as single-character JsString, but got " + x)
}
}
implicit object StringFormat extends JsonFormat[String] {
override def write(value: String, printer: JsonPrinter): Unit = printer.printString(value)
override def read(parser: JsonParser): String = parser.readString()
}
implicit object SymbolFormat extends JsonFormat[Symbol] {
override def write(value: Symbol, printer: JsonPrinter): Unit = {
printer.printString(value.name)
}
override def read(parser: JsonParser): Symbol = Symbol(parser.readString())
}
}