Skip to content

Commit 9fe9e51

Browse files
author
Dragos Manolescu
committedMay 18, 2013
Factored mapping by type name
1 parent 5103e5e commit 9fe9e51

File tree

11 files changed

+42
-82
lines changed

11 files changed

+42
-82
lines changed
 

‎build.sbt

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ libraryDependencies ++= Seq (
4646
"com.persist" %% "persist-json" % "0.13",
4747
"com.fasterxml.jackson.core" % "jackson-core" % "2.2.1",
4848
"com.fasterxml.jackson.core" % "jackson-databind" % "2.2.1",
49-
"com.fasterxml.jackson.module" % "jackson-module-scala_2.9.2" % "2.2.0",
49+
"com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.2.0",
5050
"net.liftweb" %% "lift-json" % "2.5-M4",
5151
"net.minidev" % "json-smart" % "1.1.1",
5252
"com.rojoma" %% "rojoma-json" % "2.2.0",
@@ -81,7 +81,7 @@ publishTo <<= version { (v: String) =>
8181
"snapshots" at nexus + "content/repositories/snapshots"
8282
)
8383
else Some("releases" at nexus + "service/local/staging/deploy/maven2")
84-
}
84+
}
8585

8686
publishArtifact in Test := false
8787

‎src/main/scala/com/microWorkflow/jsonScalaPerftest/LibraryAdaptor.scala

+9-2
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,21 @@ abstract class LibraryAdaptor(name: String) extends TimeMeasurements {
2222

2323
def parseOnce(json: String): Any
2424

25-
def mapOnce(json: String): Any
25+
def mapTweet(json: String): Any
26+
27+
def mapPlace(json: String) = { /* nop */ }
2628

2729
def measure(dataset: Dataset, doMap:Boolean, iterations: Int) {
2830
initialize()
2931

3032
val context2 = mainTimer.time()
3133

32-
val f: String => Any = if (doMap) mapOnce else parseOnce
34+
val mapper: String => Any = dataset.name match {
35+
case "100tweets" => mapTweet
36+
case _ => mapPlace
37+
}
38+
39+
val f: String => Any = if (doMap) mapper else parseOnce
3340

3441
try {
3542
for (count <- 1 to iterations) {
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
package com.microWorkflow.jsonScalaPerftest.jackson
22

33
import com.microWorkflow.jsonScalaPerftest.LibraryAdaptor
4-
import com.fasterxml.jackson.databind.ObjectMapper
5-
//import com.fasterxml.jackson.module.scala.DefaultScalaModule
4+
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
5+
import com.microWorkflow.jsonScalaPerftest.domain.{Place, Tweet}
66

7-
case class Url(indices: Array[Int], url: String)
8-
9-
case class HashTag(indices: Array[Int], text:String)
10-
11-
case class UserMention(indices: Array[Int], name: String)
12-
13-
case class Entities(hashtags: Array[HashTag], urls:Array[Url], user_mentions:Array[UserMention])
14-
15-
case class Tweet(id_str: String, text: String, entities: Entities)
7+
import com.fasterxml.jackson.module.scala.DefaultScalaModule
168

179
class JacksonAdaptor(name: String) extends LibraryAdaptor(name) {
1810

1911
var m: ObjectMapper = _
2012

2113
override def initialize() {
2214
m = new ObjectMapper
23-
// m.registerModule(new DefaultScalaModule)
24-
// m.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false)
15+
m.registerModule(new DefaultScalaModule)
16+
m.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
2517
}
2618

2719
override def parseOnce(json: String) = {
2820
m.readTree(json)
2921
}
3022

31-
override def mapOnce(json: String) = {
23+
override def mapTweet(json: String) = {
3224
m.readValue(json, classOf[Tweet])
3325
}
3426

27+
override def mapPlace(json: String) = {
28+
m.readValue(json, classOf[Place])
29+
}
30+
3531
override def hasMap = true
3632
}

‎src/main/scala/com/microWorkflow/jsonScalaPerftest/jsonsmart/JsonSmartAdaptor.scala

+1-17
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,6 @@ package com.microWorkflow.jsonScalaPerftest.jsonsmart
33
import com.microWorkflow.jsonScalaPerftest.LibraryAdaptor
44
import net.minidev.json.JSONValue.parse
55

6-
case class User ( utc_offset: Int
7-
, time_zone: String
8-
)
9-
10-
case class Url(indices: Array[Int], url: String)
11-
12-
case class Hashtag(indices: Array[Int], text: String)
13-
14-
case class UserMention(indices: Array[Int], name: String)
15-
16-
case class Entities(hashtags: Array[Hashtag], urls: Array[Url], userMentions: Array[UserMention])
17-
18-
case class Tweet(idStr: String, text: String, entities: Entities)
19-
20-
21-
226
class JsonSmartAdaptor(name: String) extends LibraryAdaptor(name) {
237

248
override def initialize() { /* nop */ }
@@ -27,7 +11,7 @@ class JsonSmartAdaptor(name: String) extends LibraryAdaptor(name) {
2711
parse(json /* , classOf[Tweet] */) // TODO: add map (JsonSmart v.2)
2812
}
2913

30-
def mapOnce(json: String) = null
14+
def mapTweet(json: String) = null
3115

3216
override def hasMap = false
3317

‎src/main/scala/com/microWorkflow/jsonScalaPerftest/liftjson/LiftJsonAdaptor.scala

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ package com.microWorkflow.jsonScalaPerftest.liftjson
22

33
import net.liftweb.json._
44
import com.microWorkflow.jsonScalaPerftest.LibraryAdaptor
5-
6-
case class Url(indices: Array[Int], url: String)
7-
case class Hashtag(indices: Array[Int], text: String)
8-
case class UserMention(indices: Array[Int], name: String)
9-
case class Entities(hashtags: Array[Hashtag], urls: Array[Url], userMentions: Array[UserMention])
10-
case class Tweet(id_str: String, text: String, entities: Entities)
5+
import com.microWorkflow.jsonScalaPerftest.domain.{Tweet}
116

127
class LiftJsonAdaptor(name: String) extends LibraryAdaptor(name) {
138

@@ -22,11 +17,13 @@ class LiftJsonAdaptor(name: String) extends LibraryAdaptor(name) {
2217
}
2318
}
2419

25-
override def mapOnce(json: String) = {
20+
override def mapTweet(json: String) = {
2621
implicit val formats = DefaultFormats
2722
parse(json) match {
28-
case obj: JObject => List(obj.extract[Tweet])
29-
case array: JArray => array.extract[List[Tweet]]
23+
case obj: JObject =>
24+
List(obj.extract[Tweet])
25+
case array: JArray =>
26+
array.extract[List[Tweet]]
3027
case _ => List[Tweet]()
3128
}
3229
}

‎src/main/scala/com/microWorkflow/jsonScalaPerftest/persist/PersistAdaptor.scala

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ import com.persist.JsonOps._
44
import com.persist.JsonMapper._
55

66
import com.microWorkflow.jsonScalaPerftest.LibraryAdaptor
7-
8-
case class Url(indices: Seq[Int], url: String)
9-
case class Hashtag(indices: Seq[Int], text: String)
10-
case class UserMention(indices: Seq[Int], name: String)
11-
case class Entities(hashtags: Seq[Hashtag], urls: Seq[Url], user_mentions: Seq[UserMention])
12-
case class Tweet(id_str: String, text: String, entities: Entities)
7+
import com.microWorkflow.jsonScalaPerftest.domain.{Place, Tweet}
138

149
class PersistAdaptor(name: String) extends LibraryAdaptor(name) {
1510

@@ -19,10 +14,14 @@ class PersistAdaptor(name: String) extends LibraryAdaptor(name) {
1914
Json(json)
2015
}
2116

22-
override def mapOnce(json: String) = {
17+
override def mapTweet(json: String) = {
2318
ToObject[Tweet](Json(json))
2419
}
2520

21+
override def mapPlace(json: String) = {
22+
ToObject[Place](Json(json))
23+
}
24+
2625
override def hasMap = true
2726

2827
}

‎src/main/scala/com/microWorkflow/jsonScalaPerftest/play/PlayAdaptor.scala

+2-12
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,7 @@ package com.microWorkflow.jsonScalaPerftest.play
22

33
import com.microWorkflow.jsonScalaPerftest.LibraryAdaptor
44
import play.api.libs.json._
5-
6-
case class Url(indices: Array[Int], url: String)
7-
8-
case class HashTag(indices: Array[Int], text:String)
9-
10-
case class UserMention(indices: Array[Int], name: String)
11-
12-
case class Entities(hashtags: Array[HashTag], urls:Array[Url], user_mentions:Array[UserMention])
13-
14-
case class Tweet(id_str: String, text: String, entities: Entities)
15-
5+
import com.microWorkflow.jsonScalaPerftest.domain._
166

177
class PlayAdaptor(name: String) extends LibraryAdaptor(name) {
188
implicit val urlReads = Json.reads[Url]
@@ -29,7 +19,7 @@ class PlayAdaptor(name: String) extends LibraryAdaptor(name) {
2919
Json.parse(json)
3020
}
3121

32-
def mapOnce(json: String) = {
22+
def mapTweet(json: String) = {
3323

3424
val parsed = Json.parse(json)
3525
parsed.as[Tweet]

‎src/main/scala/com/microWorkflow/jsonScalaPerftest/rojoma/RojomaAdaptor.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class RojomaAdaptor(name:String) extends LibraryAdaptor(name) {
5858

5959
def initialize() {}
6060

61-
def mapOnce(json: String) = {
61+
def mapTweet(json: String) = {
6262
JsonUtil.parseJson[Tweet](json).get
6363
}
6464

‎src/main/scala/com/microWorkflow/jsonScalaPerftest/scalalib/ScalaLibAdaptor.scala

+1-9
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@ package com.microWorkflow.jsonScalaPerftest.scalalib
33
import com.microWorkflow.jsonScalaPerftest.LibraryAdaptor
44
import scala.util.parsing.json.JSON
55

6-
/*
7-
case class Url(indices: Seq[Int], url: String)
8-
case class Hashtag(indices: Seq[Int], text: String)
9-
case class UserMention(indices: Seq[Int], name: String)
10-
case class Entities(hashtags: Seq[Hashtag], urls: Seq[Url], user_mentions: Seq[UserMention])
11-
case class Tweet(id_str: String, text: String, entities: Entities)
12-
*/
13-
146
class ScalaLibAdaptor(name: String) extends LibraryAdaptor(name) {
157

168
override def initialize() { /* nop */ }
@@ -19,7 +11,7 @@ class ScalaLibAdaptor(name: String) extends LibraryAdaptor(name) {
1911
JSON.parseFull(json)
2012
}
2113

22-
override def mapOnce(json: String) = null
14+
override def mapTweet(json: String) = null
2315

2416
override def hasMap = false
2517

‎src/main/scala/com/microWorkflow/jsonScalaPerftest/spray/SprayAdaptor.scala

+3-8
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@ package com.microWorkflow.jsonScalaPerftest.spray
22

33
import com.microWorkflow.jsonScalaPerftest.LibraryAdaptor
44
import spray.json._
5-
6-
case class Url(indices: Array[Int], url: String)
7-
case class Hashtag(indices: Array[Int], text: String)
8-
case class UserMention(indices: Array[Int], name: String)
9-
case class Entities(hashtags: Array[Hashtag], urls: Array[Url], user_mentions: Array[UserMention])
10-
case class Tweet(id_str: String, text: String, entities: Entities)
5+
import com.microWorkflow.jsonScalaPerftest.domain._
116

127
object myJsonProtocol extends DefaultJsonProtocol {
138
implicit val urlFormat = jsonFormat2(Url)
14-
implicit val hashtagFormat = jsonFormat2(Hashtag)
9+
implicit val hashtagFormat = jsonFormat2(HashTag)
1510
implicit val userMentionFormat = jsonFormat2(UserMention)
1611
implicit val entitiesFormat = jsonFormat3(Entities)
1712
implicit val tweetFormat = jsonFormat3(Tweet)
@@ -25,7 +20,7 @@ class SprayAdaptor(name: String) extends LibraryAdaptor(name) {
2520
json.asJson
2621
}
2722

28-
override def mapOnce(json: String) = {
23+
override def mapTweet(json: String) = {
2924
import myJsonProtocol._
3025
json.asJson.convertTo[Tweet]
3126
}

‎src/main/scala/com/microWorkflow/jsonScalaPerftest/twitter/TwitterAdaptor.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class TwitterAdaptor(name:String) extends LibraryAdaptor(name) {
1111
parse(json)
1212
}
1313

14-
override def mapOnce(json: String) = null
14+
override def mapTweet(json: String) = null
1515

1616
override def hasMap = false
1717

0 commit comments

Comments
 (0)
Please sign in to comment.