@@ -10,37 +10,33 @@ import java.time._
10
10
* Uses jsoniter-scala for efficient encoding and decoding.
11
11
*/
12
12
object CirceCodecs {
13
- private [this ] val numberPool = new ThreadLocal [JsonReader ] {
14
- override def initialValue (): JsonReader = new JsonReader (buf = new Array [Byte ](512 ), charBuf = new Array [Char ](512 ))
15
- }
16
- private [this ] val javaTimePool = new ThreadLocal [(Array [Byte ], JsonReader , JsonWriter )] {
17
- override def initialValue (): (Array [Byte ], JsonReader , JsonWriter ) = {
18
- val buf = new Array [Byte ](512 ) // should be enough for the longest zoned date time value
19
- buf(0 ) = '"'
20
- new Tuple3 (buf, new JsonReader (buf, charBuf = new Array [Char ](512 )), new JsonWriter (buf))
13
+ private [this ] val pool = new ThreadLocal [(JsonReader , Array [Byte ], JsonWriter )] {
14
+ override def initialValue (): (JsonReader , Array [Byte ], JsonWriter ) = {
15
+ val buf = new Array [Byte ](512 ) // should be enough for the longest number or zoned date time value
16
+ new Tuple3 (new JsonReader (buf, charBuf = new Array [Char ](512 )), buf, new JsonWriter (buf))
21
17
}
22
18
}
23
- private [this ] val readerConfig =
24
- ReaderConfig .withAppendHexDumpToParseException(false ).withPreferredBufSize(512 ).withMaxBufSize(512 )
25
- .withPreferredCharBufSize(512 ).withMaxCharBufSize(512 )
19
+ private [this ] val readerConfig = ReaderConfig .withAppendHexDumpToParseException(false )
20
+ .withPreferredBufSize(512 ).withMaxBufSize(512 ).withPreferredCharBufSize(512 ).withMaxCharBufSize(512 )
26
21
private [this ] val writeConfig = WriterConfig .withPreferredBufSize(512 )
27
22
28
23
private [this ] class ShortAsciiStringCodec [A ](codec : JsonValueCodec [A ], name : String ) extends Codec [A ] {
29
24
override def apply (x : A ): Json = {
30
- val tlb = javaTimePool .get
31
- val buf = tlb._1
25
+ val tlb = pool .get
26
+ val buf = tlb._2
32
27
io.circe.JsoniterScalaCodec .asciiStringToJString(buf, tlb._3.write(codec, x, buf, 0 , 512 , writeConfig))
33
28
}
34
29
35
30
override def apply (c : HCursor ): Decoder .Result [A ] = {
36
- val tlb = javaTimePool .get
37
- val buf = tlb._1
31
+ val tlb = pool .get
32
+ val buf = tlb._2
38
33
val s = io.circe.JsoniterScalaCodec .stringValue(c)
39
34
var len = 0
40
35
if ((s ne null ) && {
41
36
len = s.length
42
37
len <= 510
43
38
} && {
39
+ buf(0 ) = '"'
44
40
var bits, i = 0
45
41
while (i < len) {
46
42
val ch = s.charAt(i)
@@ -51,7 +47,7 @@ object CirceCodecs {
51
47
buf(i + 1 ) = '"'
52
48
bits < 0x80
53
49
}) {
54
- try return new scala.util.Right (tlb._2 .read(codec, buf, 0 , len + 2 , readerConfig))
50
+ try return new scala.util.Right (tlb._1 .read(codec, buf, 0 , len + 2 , readerConfig))
55
51
catch { case _ : JsonReaderException => }
56
52
}
57
53
error(c)
@@ -77,7 +73,7 @@ object CirceCodecs {
77
73
override def nullValue : Byte = 0
78
74
}
79
75
s =>
80
- numberPool .get.read(codec, s, readerConfig)
76
+ pool .get._1 .read(codec, s, readerConfig)
81
77
}
82
78
implicit val shortC3C : Codec [Short ] = io.circe.JsoniterScalaCodec .shortCodec {
83
79
val codec : JsonValueCodec [Short ] = new JsonValueCodec [Short ] {
@@ -95,7 +91,7 @@ object CirceCodecs {
95
91
override def nullValue : Short = 0
96
92
}
97
93
s =>
98
- numberPool .get.read(codec, s, readerConfig)
94
+ pool .get._1 .read(codec, s, readerConfig)
99
95
}
100
96
implicit val intC3C : Codec [Int ] = io.circe.JsoniterScalaCodec .intCodec {
101
97
val codec : JsonValueCodec [Int ] = new JsonValueCodec [Int ] {
@@ -113,7 +109,7 @@ object CirceCodecs {
113
109
override def nullValue : Int = 0
114
110
}
115
111
s =>
116
- numberPool .get.read(codec, s, readerConfig)
112
+ pool .get._1 .read(codec, s, readerConfig)
117
113
}
118
114
implicit val longC3C : Codec [Long ] = io.circe.JsoniterScalaCodec .longCodec {
119
115
val codec : JsonValueCodec [Long ] = new JsonValueCodec [Long ] {
@@ -131,7 +127,7 @@ object CirceCodecs {
131
127
override def nullValue : Long = 0L
132
128
}
133
129
s =>
134
- numberPool .get.read(codec, s, readerConfig)
130
+ pool .get._1 .read(codec, s, readerConfig)
135
131
}
136
132
implicit val floatC3C : Codec [Float ] = io.circe.JsoniterScalaCodec .floatCodec {
137
133
val codec : JsonValueCodec [Float ] = new JsonValueCodec [Float ] {
@@ -149,7 +145,7 @@ object CirceCodecs {
149
145
override def nullValue : Float = 0.0f
150
146
}
151
147
s =>
152
- numberPool .get.read(codec, s, readerConfig)
148
+ pool .get._1 .read(codec, s, readerConfig)
153
149
}
154
150
implicit val doubleC3C : Codec [Double ] = io.circe.JsoniterScalaCodec .doubleCodec {
155
151
val codec : JsonValueCodec [Double ] = new JsonValueCodec [Double ] {
@@ -167,7 +163,7 @@ object CirceCodecs {
167
163
override def nullValue : Double = 0.0
168
164
}
169
165
s =>
170
- numberPool .get.read(codec, s, readerConfig)
166
+ pool .get._1 .read(codec, s, readerConfig)
171
167
}
172
168
implicit val bigIntC3C : Codec [BigInt ] = io.circe.JsoniterScalaCodec .bigIntCodec {
173
169
val codec : JsonValueCodec [BigInt ] = new JsonValueCodec [BigInt ] {
@@ -185,7 +181,7 @@ object CirceCodecs {
185
181
override def nullValue : BigInt = null
186
182
}
187
183
s =>
188
- numberPool .get.read(codec, s, readerConfig)
184
+ pool .get._1 .read(codec, s, readerConfig)
189
185
}
190
186
implicit val bigDecimalC3C : Codec [BigDecimal ] = io.circe.JsoniterScalaCodec .bigDecimalCodec {
191
187
val codec : JsonValueCodec [BigDecimal ] = new JsonValueCodec [BigDecimal ] {
@@ -204,7 +200,7 @@ object CirceCodecs {
204
200
override def nullValue : BigDecimal = null
205
201
}
206
202
s =>
207
- numberPool .get.read(codec, s, readerConfig)
203
+ pool .get._1 .read(codec, s, readerConfig)
208
204
}
209
205
// codecs for java.time.* types
210
206
implicit val durationC3C : Codec [Duration ] = new ShortAsciiStringCodec (new JsonValueCodec [Duration ] {
0 commit comments