Skip to content
Browse files

booleans!

  • Loading branch information...
1 parent 1ab697b commit 4563f7d74dfe5f7d47e769866d507c289c55e49d Robey Pointer committed Aug 22, 2010
View
12 src/main/scala/com/twitter/streamyj/StreamyUnpacker.scala
@@ -104,11 +104,23 @@ class StreamyUnpacker {
}
}
+ def setBooleanField[T](obj: T, field: Field, value: Boolean) {
+ val t = field.getType
+ if (t == classOf[Boolean]) {
+ field.setBoolean(obj, value)
+ } else {
+ throw new JsonUnpackingException("Missing field conversion: " + field.getName + " of type " +
+ field.getType.toString + " missing conversion from boolean")
+ }
+ }
+
def setField[T](obj: T, field: Field, streamy: Streamy) {
streamy.next() match {
case ValueLong(x) => setLongField(obj, field, x)
case ValueDouble(x) => setDoubleField(obj, field, x)
case ValueString(x) => setStringField(obj, field, x)
+ case ValueFalse => setBooleanField(obj, field, false)
+ case ValueTrue => setBooleanField(obj, field, true)
case x =>
throw new JsonUnpackingException("Unexpected token: " + x)
View
5 src/test/scala/com/twitter/streamyj/StreamyUnpackerSpec.scala
@@ -88,5 +88,10 @@ object StreamyUnpackerSpec extends Specification {
StreamyUnpacker[BIPoint](data) mustEqual BIPoint(BigInt(50), BigDecimal(25))
}
+ "object of booleans" in {
+ case class CryForMe(cry: Boolean)
+ StreamyUnpacker[CryForMe]("""{"cry":false}""") mustEqual CryForMe(false)
+ StreamyUnpacker[CryForMe]("""{"cry":true}""") mustEqual CryForMe(true)
+ }
}
}

0 comments on commit 4563f7d

Please sign in to comment.
Something went wrong with that request. Please try again.