case classに対するcodecを自動で定義してくれる系のJSONライブラリをいくつか試してみました
コード: https://github.com/ocadaruma/benchmark-json-libs
それぞれの簡単な使い方
case class Foo(a: String, b: Double, c: Boolean)
val foo = Foo("foo", 42.0, true)
argonaut-shapeless
import argonaut.Argonaut._
import argonaut.Shapeless._
foo.asJson.nospaces // => {"c":true,"b":42.0,"a":"foo"}
"""{"c":true,"b":42.0,"a":"foo"}""".decode[Foo].toOption // => Some(Foo(foo,42.0,true))
circe
import io.circe.parser._
import io.circe.syntax._
import io.circe.generic.auto._
foo.asJson.noSpaces // => {"a":"foo","b":42.0,"c":true}
decode[Foo]("""{"a":"foo","b":42.0,"c":true}""").toOption // => Some(Foo(foo,42.0,true))
JSON4S
import org.json4s._
import org.json4s.jackson.{Serialization, JsonMethods}
implicit val formats = DefaultFormats
Serialization.write(foo) // => {"a":"foo","b":42.0,"c":true}
JsonMethods.parse("""{"a":"foo","b":42.0,"c":true}""").extract[Foo] // => Foo(foo,42.0,true)
spray-json-shapeless
import spray.json._
import fommil.sjs.FamilyFormats._
foo.toJson.compactPrint // => {"a":"foo","b":42.0,"c":true}
"""{"a":"foo","b":42.0,"c":true}""".parseJson.convertTo[Foo] // => Foo(foo,42.0,true)
ベンチマーク結果
sbt-jmh使いました。
[info] Benchmark Mode Cnt Score Error Units
[info] BenchmarkJsonLibs.argonautShapelessDeserialize thrpt 10 41.063 ± 1.909 ops/s
[info] BenchmarkJsonLibs.argonautShapelessSerialize thrpt 10 37.530 ± 0.926 ops/s
[info] BenchmarkJsonLibs.circeDeserialize thrpt 10 52.426 ± 4.896 ops/s
[info] BenchmarkJsonLibs.circeSerialize thrpt 10 46.619 ± 1.056 ops/s
[info] BenchmarkJsonLibs.json4sJacksonDeserialize thrpt 10 50.363 ± 1.359 ops/s
[info] BenchmarkJsonLibs.json4sJacksonSerialize thrpt 10 29.081 ± 0.509 ops/s
[info] BenchmarkJsonLibs.json4sNativeDeserialize thrpt 10 37.430 ± 1.234 ops/s
[info] BenchmarkJsonLibs.json4sNativeSerialize thrpt 10 28.723 ± 0.934 ops/s
[info] BenchmarkJsonLibs.sprayJsonShapelessDeserialize thrpt 10 70.349 ± 2.470 ops/s
[info] BenchmarkJsonLibs.sprayJsonShapelessSerialize thrpt 10 29.165 ± 0.468 ops/s
Serializeはcirceがもっとも速く、Deserializeではspray-json-shapelessが一番速いという結果になりました。
今度Parserを変えていろいろ試してみよう