If the field contains an array or an object, it returns their json representation, you can chain these calls to access a leaf lower in the json tree.
Here are a few benchmarks ran on a json file from facebook API, 396Kb:
First
uses a methoddeserializeToMap
that uses Jackson object mapper to extract a Map[String, Any] (not shown here)Second
uses the stream parser
Even for short json this is still worth it. I guess that mapping to an object instead of using the deserializeToMap
would be even slower.
This might not be fair as the map approach loads the whole file in memory first.
def stream = {
val json = new java.io.FileInputStream("/tmp/test.json")
ScalaJson.getFieldOnly("test", json)
}
def map={
val json:String = scala.io.Source.fromFile("/tmp/test.json").mkString
val m = ScalaJson.deserializeToMap(json)
m("test")
}
scala> th.pbenchOff() {map}{stream}
Benchmark comparison (in 16.15 s)
Significantly different (p ~= 0)
Time ratio: 0.00042 95% CI 0.00040 - 0.00044 (n=20)
First 28.14 ms 95% CI 27.66 ms - 28.61 ms
Second 11.83 us 95% CI 11.37 us - 12.29 us
scala> th.pbenchOff() {map}{stream}
Benchmark comparison (in 8.259 s)
Significantly different (p ~= 0)
Time ratio: 0.04471 95% CI 0.04068 - 0.04873 (n=20)
First 32.45 ms 95% CI 30.75 ms - 34.15 ms
Second 1.451 ms 95% CI 1.344 ms - 1.557 ms
This might be fairer for cases where the json is already in memory.
val json:String = scala.io.Source.fromFile("/tmp/test.json").mkString
def stream(j: String) = {
ScalaJson.getFieldOnly("test", j)
}
def map(j: String)={
val m = ScalaJson.deserializeToMap(j)
m("test")
}
scala> th.pbenchOff() {map(json)}{stream(json)}
Benchmark comparison (in 8.818 s)
Significantly different (p ~= 0)
Time ratio: 0.00018 95% CI 0.00017 - 0.00018 (n=20)
First 1.647 ms 95% CI 1.634 ms - 1.659 ms
Second 289.7 ns 95% CI 286.6 ns - 292.8 ns
scala> th.pbenchOff() {try{map(json)} catch{case _ =>}}{try{stream(json)} catch{case _ =>}}
Benchmark comparison (in 9.264 s)
Significantly different (p ~= 0)
Time ratio: 0.80233 95% CI 0.79113 - 0.81353 (n=20)
First 1.685 ms 95% CI 1.670 ms - 1.700 ms
Second 1.352 ms 95% CI 1.337 ms - 1.367 ms
scala> th.pbenchOff() {map(json)}{stream(json)}
Benchmark comparison (in 9.183 s)
Significantly different (p ~= 0)
Time ratio: 0.82000 95% CI 0.81205 - 0.82795 (n=20)
First 1.651 ms 95% CI 1.641 ms - 1.662 ms
Second 1.354 ms 95% CI 1.344 ms - 1.364 ms
val json = """{
"id": "14975",
"from": {
"category": "Movie",
"name": "Anchorman",
},
"message": "Just realized that Facebook is NOT a personal journal for private thoughts."
}"""
scala> th.pbenchOff() {map(json)}{stream(json)}
Benchmark comparison (in 9.888 s)
Significantly different (p ~= 0)
Time ratio: 0.21309 95% CI 0.21096 - 0.21522 (n=20)
First 1.639 us 95% CI 1.629 us - 1.650 us
Second 349.3 ns 95% CI 346.7 ns - 352.0 ns
scala> th.pbenchOff() {try{map(json)} catch{case _ =>}}{try{stream(json)} catch{case _ =>}}
Benchmark comparison (in 11.12 s)
Significantly different (p ~= 0)
Time ratio: 0.16361 95% CI 0.16248 - 0.16474 (n=20)
First 5.794 us 95% CI 5.762 us - 5.826 us
Second 947.9 ns 95% CI 944.0 ns - 951.9 ns
scala> th.pbenchOff() {map(json)}{stream(json)}
Benchmark comparison (in 16.82 s)
Significantly different (p ~= 0)
Time ratio: 0.63468 95% CI 0.62477 - 0.64458 (n=20)
First 1.591 us 95% CI 1.578 us - 1.605 us
Second 1.010 us 95% CI 996.7 ns - 1.023 us