1
1
package com .github .plokhotnyuk .jsoniter_scala .benchmark
2
2
3
3
import spray .json ._
4
+
4
5
import java .time ._
5
6
import java .util .concurrent .ConcurrentHashMap
6
7
import java .util .{Base64 , UUID }
7
- import scala .collection .immutable .{ArraySeq , Map }
8
+ import scala .collection .immutable .{ArraySeq , Map , TreeMap }
8
9
import scala .collection .mutable
9
10
import scala .reflect .ClassTag
10
11
@@ -1256,14 +1257,14 @@ object SprayFormats extends DefaultJsonProtocol {
1256
1257
else {
1257
1258
val es = json.asInstanceOf [JsArray ].elements
1258
1259
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 ]) )
1260
1261
buf
1261
1262
}
1262
1263
1263
1264
def write (buf : mutable.ArrayBuffer [T ]): JsValue = {
1264
1265
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())
1267
1268
}
1268
1269
}
1269
1270
@@ -1272,23 +1273,30 @@ object SprayFormats extends DefaultJsonProtocol {
1272
1273
def read (json : JsValue ): ArraySeq [T ] = json match {
1273
1274
case ja : JsArray =>
1274
1275
val b = ArraySeq .newBuilder[T ]
1275
- ja.elements.foreach(e => b += e.convertTo[T ])
1276
+ ja.elements.foreach(e => b.addOne( e.convertTo[T ]) )
1276
1277
b.result()
1277
1278
case _ => deserializationError(s " Expected JSON array " )
1278
1279
}
1279
1280
1280
1281
def write (as : ArraySeq [T ]): JsValue = {
1281
1282
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())
1284
1285
}
1285
1286
}
1286
1287
1287
1288
private [this ] def toJson [T : JsonWriter ](x : T , d : T ): JsValue =
1288
1289
if (x == d) JsNull
1289
1290
else x.toJson
1290
1291
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
+ }
1294
1302
}
0 commit comments