11package com .github .plokhotnyuk .jsoniter_scala .benchmark
22
33import spray .json ._
4+
45import java .time ._
56import java .util .concurrent .ConcurrentHashMap
67import java .util .{Base64 , UUID }
7- import scala .collection .immutable .{ArraySeq , Map }
8+ import scala .collection .immutable .{ArraySeq , Map , TreeMap }
89import scala .collection .mutable
910import scala .reflect .ClassTag
1011
@@ -1256,14 +1257,14 @@ object SprayFormats extends DefaultJsonProtocol {
12561257 else {
12571258 val es = json.asInstanceOf [JsArray ].elements
12581259 val buf = new mutable.ArrayBuffer [T ](es.size)
1259- es.foreach(e => buf += e.convertTo[T ])
1260+ es.foreach(e => buf.addOne( e.convertTo[T ]) )
12601261 buf
12611262 }
12621263
12631264 def write (buf : mutable.ArrayBuffer [T ]): JsValue = {
12641265 val vs = Vector .newBuilder[JsValue ]
1265- buf.foreach(x => vs += x.toJson)
1266- JsArray (vs.result())
1266+ buf.foreach(x => vs.addOne( x.toJson) )
1267+ new JsArray (vs.result())
12671268 }
12681269 }
12691270
@@ -1272,23 +1273,30 @@ object SprayFormats extends DefaultJsonProtocol {
12721273 def read (json : JsValue ): ArraySeq [T ] = json match {
12731274 case ja : JsArray =>
12741275 val b = ArraySeq .newBuilder[T ]
1275- ja.elements.foreach(e => b += e.convertTo[T ])
1276+ ja.elements.foreach(e => b.addOne( e.convertTo[T ]) )
12761277 b.result()
12771278 case _ => deserializationError(s " Expected JSON array " )
12781279 }
12791280
12801281 def write (as : ArraySeq [T ]): JsValue = {
12811282 val vs = Vector .newBuilder[JsValue ]
1282- as.foreach(x => vs += x.toJson)
1283- JsArray (vs.result())
1283+ as.foreach(x => vs.addOne( x.toJson) )
1284+ new JsArray (vs.result())
12841285 }
12851286 }
12861287
12871288 private [this ] def toJson [T : JsonWriter ](x : T , d : T ): JsValue =
12881289 if (x == d) JsNull
12891290 else x.toJson
12901291
1291- private [this ] def toJsObject (fields : (String , JsValue )* ): JsObject = JsObject (fields.filterNot { case (_, v) =>
1292- (v eq JsNull ) || (v.isInstanceOf [JsArray ] && v.asInstanceOf [JsArray ].elements.isEmpty)
1293- }:_* )
1292+ private [this ] def toJsObject (fields : (String , JsValue )* ): JsObject = JsObject {
1293+ val builder = TreeMap .newBuilder[String , JsValue ](Ordering .String )
1294+ val it = fields.iterator
1295+ while (it.hasNext) {
1296+ val kv = it.next()
1297+ val v = kv._2
1298+ if (! ((v eq JsNull ) || (v.isInstanceOf [JsArray ] && v.asInstanceOf [JsArray ].elements.isEmpty))) builder.addOne(kv)
1299+ }
1300+ builder.result()
1301+ }
12941302}
0 commit comments