Skip to content

Commit 0cea612

Browse files
committed
Code clean up
1 parent 040f189 commit 0cea612

File tree

1 file changed

+20
-24
lines changed
  • jsoniter-scala-circe/shared/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/circe

1 file changed

+20
-24
lines changed

jsoniter-scala-circe/shared/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/circe/CirceCodecs.scala

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,33 @@ import java.time._
1010
* Uses jsoniter-scala for efficient encoding and decoding.
1111
*/
1212
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))
2117
}
2218
}
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)
2621
private[this] val writeConfig = WriterConfig.withPreferredBufSize(512)
2722

2823
private[this] class ShortAsciiStringCodec[A](codec: JsonValueCodec[A], name: String) extends Codec[A] {
2924
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
3227
io.circe.JsoniterScalaCodec.asciiStringToJString(buf, tlb._3.write(codec, x, buf, 0, 512, writeConfig))
3328
}
3429

3530
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
3833
val s = io.circe.JsoniterScalaCodec.stringValue(c)
3934
var len = 0
4035
if ((s ne null) && {
4136
len = s.length
4237
len <= 510
4338
} && {
39+
buf(0) = '"'
4440
var bits, i = 0
4541
while (i < len) {
4642
val ch = s.charAt(i)
@@ -51,7 +47,7 @@ object CirceCodecs {
5147
buf(i + 1) = '"'
5248
bits < 0x80
5349
}) {
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))
5551
catch { case _: JsonReaderException => }
5652
}
5753
error(c)
@@ -77,7 +73,7 @@ object CirceCodecs {
7773
override def nullValue: Byte = 0
7874
}
7975
s =>
80-
numberPool.get.read(codec, s, readerConfig)
76+
pool.get._1.read(codec, s, readerConfig)
8177
}
8278
implicit val shortC3C: Codec[Short] = io.circe.JsoniterScalaCodec.shortCodec {
8379
val codec: JsonValueCodec[Short] = new JsonValueCodec[Short] {
@@ -95,7 +91,7 @@ object CirceCodecs {
9591
override def nullValue: Short = 0
9692
}
9793
s =>
98-
numberPool.get.read(codec, s, readerConfig)
94+
pool.get._1.read(codec, s, readerConfig)
9995
}
10096
implicit val intC3C: Codec[Int] = io.circe.JsoniterScalaCodec.intCodec {
10197
val codec: JsonValueCodec[Int] = new JsonValueCodec[Int] {
@@ -113,7 +109,7 @@ object CirceCodecs {
113109
override def nullValue: Int = 0
114110
}
115111
s =>
116-
numberPool.get.read(codec, s, readerConfig)
112+
pool.get._1.read(codec, s, readerConfig)
117113
}
118114
implicit val longC3C: Codec[Long] = io.circe.JsoniterScalaCodec.longCodec {
119115
val codec: JsonValueCodec[Long] = new JsonValueCodec[Long] {
@@ -131,7 +127,7 @@ object CirceCodecs {
131127
override def nullValue: Long = 0L
132128
}
133129
s =>
134-
numberPool.get.read(codec, s, readerConfig)
130+
pool.get._1.read(codec, s, readerConfig)
135131
}
136132
implicit val floatC3C: Codec[Float] = io.circe.JsoniterScalaCodec.floatCodec {
137133
val codec: JsonValueCodec[Float] = new JsonValueCodec[Float] {
@@ -149,7 +145,7 @@ object CirceCodecs {
149145
override def nullValue: Float = 0.0f
150146
}
151147
s =>
152-
numberPool.get.read(codec, s, readerConfig)
148+
pool.get._1.read(codec, s, readerConfig)
153149
}
154150
implicit val doubleC3C: Codec[Double] = io.circe.JsoniterScalaCodec.doubleCodec {
155151
val codec: JsonValueCodec[Double] = new JsonValueCodec[Double] {
@@ -167,7 +163,7 @@ object CirceCodecs {
167163
override def nullValue: Double = 0.0
168164
}
169165
s =>
170-
numberPool.get.read(codec, s, readerConfig)
166+
pool.get._1.read(codec, s, readerConfig)
171167
}
172168
implicit val bigIntC3C: Codec[BigInt] = io.circe.JsoniterScalaCodec.bigIntCodec {
173169
val codec: JsonValueCodec[BigInt] = new JsonValueCodec[BigInt] {
@@ -185,7 +181,7 @@ object CirceCodecs {
185181
override def nullValue: BigInt = null
186182
}
187183
s =>
188-
numberPool.get.read(codec, s, readerConfig)
184+
pool.get._1.read(codec, s, readerConfig)
189185
}
190186
implicit val bigDecimalC3C: Codec[BigDecimal] = io.circe.JsoniterScalaCodec.bigDecimalCodec {
191187
val codec: JsonValueCodec[BigDecimal] = new JsonValueCodec[BigDecimal] {
@@ -204,7 +200,7 @@ object CirceCodecs {
204200
override def nullValue: BigDecimal = null
205201
}
206202
s =>
207-
numberPool.get.read(codec, s, readerConfig)
203+
pool.get._1.read(codec, s, readerConfig)
208204
}
209205
// codecs for java.time.* types
210206
implicit val durationC3C: Codec[Duration] = new ShortAsciiStringCodec(new JsonValueCodec[Duration] {

0 commit comments

Comments
 (0)