Permalink
Browse files

many changes to make more flexible and consistent

  • Loading branch information...
1 parent d8fedee commit 7d8ba565808210df2c07222851656cb741d3bd5c Jeremy Cloud committed Dec 14, 2010
View
@@ -4,3 +4,4 @@ project/target
project/plugins/project/
project/plugins/src_managed/
target
+streamyj.tmproj
View
@@ -19,62 +19,43 @@ on specific parsed items.
It's easiest to show with an example
val s = new Streamy("""{"bar":1}""")
- s.obj {
- case FieldName("bar") => println(s.readField())
+ s readObject {
+ case "bar" => println(s.readScalar())
}
-In this case, we call obj() on the Streamy object to tell the parser to read the current object.
+In this case, we call readObject() on the Streamy object to tell the parser to read the current object.
The case statements tell Streamy to print the current field value when the field name is "bar".
Simple enough.
A slightly more complex example
var baz = 0L
val s = new Streamy("""{"bar":{"baz":1}}""")
- s.obj {
- case FieldName("bar") => {
- s.obj {
- case FieldName("baz") => baz = s.readLongField()
- }
+ s readObject {
+ case "bar" => s readObject {
+ case "baz" => baz = s.readLong()
}
}
Here we have nested handlers. When the parser encounters the "bar" field, we tell it
to read another object. In this nested object, when you see the baz field, assign our
baz var whatever the value of the baz field is in JSON.
-Parsing arrays is also handled.
+Parsing arrays is also handled. Instead of field names when reading an object,
+the readArray takes a function that takes the current index in the array.
val s = new Streamy("""{"bar":[1,2,3]}""")
- s.obj {
- case FieldName("bar") => {
- s.arr {
- case ValueLong(l) => println(l)
- }
+ s readObject {
+ case "bar" => s readArray {
+ idx => println(idx + ": " + s.readLong())
}
}
Parsing methods can also be defined and used without the {} syntax
- val printfield:Streamy.ParseFunc = {
- case FieldName(s) => println("hit field " + s)
+ val printfield: Streamy.ObjectParseFunc = {
+ case s => println("hit field " + s)
}
val s = new Streamy("""{"bar":1, "baz":2}""")
- s.obj(printfield)
+ s.readObject(printfield)
-Lastly, if the helpers fail you always have access to the underlying Json reader,
-as well as the current Streamy token
-
- val s = new Streamy("""{"bar": 1, "baz": {"baz2": 3}}""")
- s.obj {
- case FieldName("bar") => println("bar is " + s.readLongField())
- case FieldName("baz") => {
- println(s.next())
- s.token match {
- case StartObject => println("I'm at an object start")
- case _ => println("I'm not at an object start")
- }
- s.readObject(s.eat)
- }
- }
-
View
@@ -3,6 +3,6 @@
project.organization=com.twitter
project.name=streamyj
sbt.version=0.7.4
-project.version=0.1-SNAPSHOT
-build.scala.versions=2.8.0 2.7.7
+project.version=0.2-SNAPSHOT
+build.scala.versions=2.8.1 2.7.7
project.initialize=false
@@ -7,7 +7,7 @@ class StreamyJProject(info: ProjectInfo) extends StandardProject(info) with Subv
val objenesis = "org.objenesis" % "objenesis" % "1.1"
val specs = buildScalaVersion match {
case "2.7.7" => "org.scala-tools.testing" % "specs" % "1.6.2.1" % "test"
- case _ => "org.scala-tools.testing" %% "specs" % "1.6.5" % "test"
+ case _ => "org.scala-tools.testing" %% "specs" % "1.6.6" % "test"
}
override def disableCrossPaths = false
@@ -2,5 +2,5 @@ import sbt._
class Plugins(info: ProjectInfo) extends PluginDefinition(info) {
val twitterMaven = "twitter.com" at "http://maven.twttr.com/"
- val defaultProject = "com.twitter" % "standard-project" % "0.7.10"
+ val defaultProject = "com.twitter" % "standard-project" % "0.7.17"
}
Oops, something went wrong.

0 comments on commit 7d8ba56

Please sign in to comment.