Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Some cleanup

  • Loading branch information...
commit 2ebdd9fd40fbdc695ab7e0fac7da54d32cbbf3e3 1 parent 17d49f1
@jorgeortiz85 jorgeortiz85 authored
View
37 src/main/scala/com/foursquare/dynamicjson/DynamicJsonTraverser.scala
@@ -1,37 +0,0 @@
-// package com.foursquare.json.traverser
-//
-// import net.liftweb.json.JsonAST._
-//
-// case class DynamicJsonTraverser(getTraverser: JsonTraverser) extends JsonTraverser with Dynamic {
-// def this(json: JValue) =
-// this(new JsonTraverserImpl(json))
-//
-// override def getJson: JValue =
-// getTraverser.getJson
-//
-// override def getError: Option[JsonError] =
-// getTraverser.getError
-//
-// override def getResult[T: ResultType]: Option[T] =
-// getTraverser.getResult[T]
-//
-// override def getIndex(index: Int): DynamicJsonTraverser =
-// DynamicJsonTraverser(getTraverser.getIndex(index))
-//
-// override def getKey(name: String): DynamicJsonTraverser =
-// DynamicJsonTraverser(getTraverser.getKey(name))
-//
-// def apply(index: Int): DynamicJsonTraverser =
-// getIndex(index)
-//
-// def applyDynamic(name: String)(indexes: Int*): DynamicJsonTraverser = {
-// if (indexes.isEmpty)
-// getKey(name)
-// else
-// getKey(name).getIndex(indexes.head)
-// }
-// }
-//
-// object DynamicJsonTraverser {
-// def apply(json: JValue): DynamicJsonTraverser = new DynamicJsonTraverser(json)
-// }
View
0  .../scala/com/foursquare/dynamicjson/JsonError.scala → ...ala/com/foursquare/json/traverser/JsonError.scala
File renamed without changes
View
10 ...la/com/foursquare/dynamicjson/JsonTraverser.scala → ...com/foursquare/json/traverser/JsonTraverser.scala
@@ -3,15 +3,13 @@ package com.foursquare.json.traverser
import net.liftweb.json.JsonAST.JValue
trait JsonTraverser {
- def getJson: JValue
- def getError: Option[JsonError]
- def getResult[T: ResultType]: Option[T] = {
+ def json: JValue
+ def error: Option[JsonError]
+ def result[T: ResultType]: Option[T] = {
val resultType = implicitly[ResultType[T]]
- resultType.parse(getJson)
+ resultType.parse(json)
}
- // def getIndex(index: Int): JsonTraverser
- // def getKey(name: String): JsonTraverser
def apply(index: Int): JsonTraverser
def apply(name: String): JsonTraverser
}
View
6 ...ursquare/dynamicjson/JsonTraverserErrorImpl.scala → ...quare/json/traverser/JsonTraverserErrorImpl.scala
@@ -2,8 +2,8 @@ package com.foursquare.json.traverser
import net.liftweb.json.JsonAST.{JValue, JNothing}
-private case class JsonTraverserErrorImpl(override val getError: Option[JsonError]) extends JsonTraverser {
- override def getJson: JValue = JNothing
+private case class JsonTraverserErrorImpl(override val error: Option[JsonError]) extends JsonTraverser {
+ override def json: JValue = JNothing
// We propagate only the first error
override def apply(index: Int): JsonTraverser = this
@@ -11,5 +11,5 @@ private case class JsonTraverserErrorImpl(override val getError: Option[JsonErro
// We propagate only the first error
override def apply(name: String): JsonTraverser = this
- override def getResult[T: ResultType]: Option[T] = None
+ override def result[T: ResultType]: Option[T] = None
}
View
14 ...om/foursquare/dynamicjson/JsonTraverserImpl.scala → ...foursquare/json/traverser/JsonTraverserImpl.scala
@@ -2,29 +2,29 @@ package com.foursquare.json.traverser
import net.liftweb.json.JsonAST.{JValue, JArray, JNothing}
-private case class JsonTraverserImpl(override val getJson: JValue) extends JsonTraverser {
- override def getError: Option[JsonError] = None
+private case class JsonTraverserImpl(override val json: JValue) extends JsonTraverser {
+ override def error: Option[JsonError] = None
- override def apply(index: Int): JsonTraverser = getJson match {
+ override def apply(index: Int): JsonTraverser = json match {
case JArray(arr) if arr.size > index =>
new JsonTraverserImpl(arr(index))
case JArray(arr) =>
val msg = "Attempt to access index " + index + " in array of length " + arr.size
- val error = JsonError(msg, getJson)
+ val error = JsonError(msg, json)
new JsonTraverserErrorImpl(Some(error))
case _ =>
- val error = JsonError("Json value is not an array", getJson)
+ val error = JsonError("Json value is not an array", json)
new JsonTraverserErrorImpl(Some(error))
}
override def apply(name: String): JsonTraverser = {
- val newJson = getJson \ name
+ val newJson = json \ name
newJson match {
case JNothing =>
- val error = JsonError("Attempt to access nonexistant field: " + name, getJson)
+ val error = JsonError("Attempt to access nonexistant field: " + name, json)
new JsonTraverserErrorImpl(Some(error))
case _ =>
View
0  ...scala/com/foursquare/dynamicjson/ResultType.scala → ...la/com/foursquare/json/traverser/ResultType.scala
File renamed without changes
View
33 src/main/scala/com/foursquare/dynamicjson/Test.scala → ...foursquare/json/traverser/JsonTraverserTest.scala
@@ -1,40 +1,19 @@
-import com.foursquare.json.traverser.JsonTraverser //, DynamicJsonTraverser}
+import com.foursquare.json.traverser.JsonTraverser
import net.liftweb.json.JsonParser
-object Test {
+object JsonTraverserTest {
def main(args: Array[String]): Unit = {
val jsonStr = """{"id_str":"171331379555090432","coordinates":{"coordinates":[-73.77090964,40.75205260],"type":"Point"},"in_reply_to_status_id_str":"171331036926586883","place":{"name":"Queens","attributes":{},"full_name":"Queens, NY","place_type":"city","country":"United States","bounding_box":{"coordinates":[[[-74.042112,40.489794],[-73.700272,40.489794],[-73.700272,40.812242],[-74.042112,40.812242]]],"type":"Polygon"},"id":"b6ea2e341ba4356f","url":"http:\/\/api.twitter.com\/1\/geo\/id\/b6ea2e341ba4356f.json","country_code":"US"},"geo":{"coordinates":[40.75205260,-73.77090964],"type":"Point"},"in_reply_to_user_id_str":"3187821","favorited":false,"created_at":"Sun Feb 19 20:32:38 +0000 2012","user":{"id":3032241,"profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/4689671\/bg.png","created_at":"Sat Mar 31 02:11:20 +0000 2007","profile_image_url":"http:\/\/a3.twimg.com\/profile_images\/1776174244\/image1327348450_normal.png","profile_text_color":"663B12","time_zone":"Eastern Time (US & Canada)","following":true,"followers_count":1327,"profile_sidebar_border_color":"C6E2EE","screen_name":"bdotdub","verified":false,"utc_offset":-18000,"default_profile_image":false,"location":"New York, NY","name":"Benny Wong","notifications":false,"profile_background_tile":true,"favourites_count":207,"id_str":"3032241","profile_sidebar_fill_color":"c2e4a0","contributors_enabled":false,"protected":false,"show_all_inline_media":true,"profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1776174244\/image1327348450_normal.png","profile_background_color":"c4edc8","description":"building time machine as cofounder of @timehop) w\/ @jwegener \u2022 built @ExitStrategyNYC iPhone app \u2022 formerly built @GiltCity","default_profile":false,"lang":"en","is_translator":false,"profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/4689671\/bg.png","statuses_count":8596,"geo_enabled":true,"profile_link_color":"18b500","follow_request_sent":false,"friends_count":546,"listed_count":84,"profile_use_background_image":true,"url":"http:\/\/bwong.net"},"in_reply_to_screen_name":"jwegener","in_reply_to_user_id":3187821,"retweeted":false,"in_reply_to_status_id":171331036926586883,"contributors":null,"truncated":false,"source":"\u003Ca href=\"http:\/\/twitter.com\/#!\/download\/iphone\" rel=\"nofollow\"\u003ETwitter for iPhone\u003C\/a\u003E","id":171331379555090432,"retweet_count":0,"text":"@jwegener @msingleton @ericfriedman agrizzle. Eric was building subscribetoit for @msg"}"""
val json = JsonParser.parse(jsonStr)
- // Using Lift extractors
- implicit val formats = net.liftweb.json.DefaultFormats
- val coords1 = json \ "geo" \ "coordinates"
- val xy1 =
- try {
- val x = coords1.children(0).extract[Double]
- val y = coords1.children(1).extract[Double]
- Some((x, y))
- } catch {
- case ex: Exception => None
- }
-
// Using JsonTraverser
- val tjson = JsonTraverser(json)
- val coords2 = tjson("geo")("coordinates")
+ val jsont = JsonTraverser(json)
+ val coords2 = jsont("geo")("coordinates")
val xy2 =
for {
- x <- coords2(0).getResult[Double]
- y <- coords2(1).getResult[Double]
+ x <- coords2(0).result[Double]
+ y <- coords2(1).result[Double]
} yield (x, y)
-
- // Using DynamicJsonTraverser
- // val djson = DynamicJsonTraverser(json)
- // val coords3 = djson.geo.coordinates
- // val xy3 =
- // for {
- // x <- coords3(0).getResult[Double]
- // y <- coords3(1).getResult[Double]
- // } yield (x, y)
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.