Permalink
Browse files

add support for double and string.

  • Loading branch information...
1 parent 3a75c04 commit 1ab697b2c75ad5b02cbe38c785c24bfca23a25f6 Robey Pointer committed Aug 22, 2010
View
80 src/main/scala/com/twitter/streamyj/StreamyUnpacker.scala
@@ -32,34 +32,96 @@ class StreamyUnpacker {
field.setChar(obj, value.toChar)
} else if (t == classOf[Byte]) {
field.setByte(obj, value.toByte)
+ } else if (t == classOf[Float]) {
+ field.setFloat(obj, value.toFloat)
+ } else if (t == classOf[Double]) {
+ field.setDouble(obj, value.toDouble)
+ } else if (t == classOf[String]) {
+ field.set(obj, value.toString)
+ } else if (t == classOf[BigInt]) {
+ field.set(obj, BigInt(value))
+ } else if (t == classOf[BigDecimal]) {
+ field.set(obj, BigDecimal(value))
} else {
throw new JsonUnpackingException("Missing field conversion: " + field.getName + " of type " +
field.getType.toString + " missing conversion from long")
}
}
+ def setDoubleField[T](obj: T, field: Field, value: Double) {
+ val t = field.getType
+ if (t == classOf[Int]) {
+ field.setInt(obj, value.toInt)
+ } else if (t == classOf[Long]) {
+ field.setLong(obj, value.toLong)
+ } else if (t == classOf[Short]) {
+ field.setShort(obj, value.toShort)
+ } else if (t == classOf[Char]) {
+ field.setChar(obj, value.toChar)
+ } else if (t == classOf[Byte]) {
+ field.setByte(obj, value.toByte)
+ } else if (t == classOf[Float]) {
+ field.setFloat(obj, value.toFloat)
+ } else if (t == classOf[Double]) {
+ field.setDouble(obj, value)
+ } else if (t == classOf[String]) {
+ field.set(obj, value.toString)
+ } else if (t == classOf[BigInt]) {
+ field.set(obj, BigInt(value.toLong))
+ } else if (t == classOf[BigDecimal]) {
+ field.set(obj, BigDecimal(value))
+ } else {
+ throw new JsonUnpackingException("Missing field conversion: " + field.getName + " of type " +
+ field.getType.toString + " missing conversion from double")
+ }
+ }
+
+ def setStringField[T](obj: T, field: Field, value: String) {
+ val t = field.getType
+ if (t == classOf[Int]) {
+ field.setInt(obj, value.toInt)
+ } else if (t == classOf[Long]) {
+ field.setLong(obj, value.toLong)
+ } else if (t == classOf[Short]) {
+ field.setShort(obj, value.toShort)
+ } else if (t == classOf[Char]) {
+ field.setChar(obj, value.toInt.toChar)
+ } else if (t == classOf[Byte]) {
+ field.setByte(obj, value.toByte)
+ } else if (t == classOf[Float]) {
+ field.setFloat(obj, value.toFloat)
+ } else if (t == classOf[Double]) {
+ field.setDouble(obj, value.toDouble)
+ } else if (t == classOf[String]) {
+ field.set(obj, value)
+ } else if (t == classOf[BigInt]) {
+ field.set(obj, BigInt(value))
+ } else if (t == classOf[BigDecimal]) {
+ field.set(obj, BigDecimal(value))
+ } else {
+ throw new JsonUnpackingException("Missing field conversion: " + field.getName + " of type " +
+ field.getType.toString + " missing conversion from string")
+ }
+ }
+
def setField[T](obj: T, field: Field, streamy: Streamy) {
streamy.next() match {
- case ValueLong(x) =>
- setLongField(obj, field, x)
+ case ValueLong(x) => setLongField(obj, field, x)
+ case ValueDouble(x) => setDoubleField(obj, field, x)
+ case ValueString(x) => setStringField(obj, field, x)
+ case x =>
+ throw new JsonUnpackingException("Unexpected token: " + x)
/*
case object StartArray extends StreamyToken
case object EndArray extends StreamyToken
case object StartObject extends StreamyToken
case object EndObject extends StreamyToken
- case class FieldName(name: String) extends StreamyToken
- case object NotAvailable extends StreamyToken
case object ValueFalse extends StreamyToken
case object ValueTrue extends StreamyToken
case object ValueNull extends StreamyToken
- case class ValueDouble(override val value: Double) extends ValueScalar(value)
- case class ValueString(override val value: String) extends ValueScalar(value)
*/
-// case x: BigDecimal =>
-// case x: Boolean =>
-// case x: String =>
// null
// array, object
}
View
63 src/test/scala/com/twitter/streamyj/StreamyUnpackerSpec.scala
@@ -23,9 +23,70 @@ import scala.collection.immutable
object StreamyUnpackerSpec extends Specification {
"StreamyUnpacker" should {
"object of ints" in {
+ val data = """{"x":50,"y":25}"""
+
+ case class Point(x: Int, y: Int)
+ StreamyUnpacker[Point](data) mustEqual Point(50, 25)
+
+ case class LPoint(x: Long, y: Int)
+ StreamyUnpacker[LPoint](data) mustEqual LPoint(50L, 25)
+
+ case class BPoint(x: Byte, y: Byte)
+ StreamyUnpacker[BPoint](data) mustEqual BPoint(50.toByte, 25.toByte)
+
+ case class FPoint(x: Float, y: Double)
+ StreamyUnpacker[FPoint](data) mustEqual FPoint(50.0f, 25.0)
+
+ case class SPoint(x: String, y: Int)
+ StreamyUnpacker[SPoint](data) mustEqual SPoint("50", 25)
+
+ case class BIPoint(x: BigInt, y: BigDecimal)
+ StreamyUnpacker[BIPoint](data) mustEqual BIPoint(BigInt(50), BigDecimal(25))
+ }
+
+ "object of doubles" in {
+ val data = """{"x":50.0,"y":25.0}"""
+
+ case class Point(x: Int, y: Int)
+ StreamyUnpacker[Point](data) mustEqual Point(50, 25)
+
+ case class LPoint(x: Long, y: Int)
+ StreamyUnpacker[LPoint](data) mustEqual LPoint(50L, 25)
+
+ case class BPoint(x: Byte, y: Byte)
+ StreamyUnpacker[BPoint](data) mustEqual BPoint(50.toByte, 25.toByte)
+
+ case class FPoint(x: Float, y: Double)
+ StreamyUnpacker[FPoint](data) mustEqual FPoint(50.0f, 25.0)
+
+ case class SPoint(x: String, y: Int)
+ StreamyUnpacker[SPoint](data) mustEqual SPoint("50.0", 25)
+
+ case class BIPoint(x: BigInt, y: BigDecimal)
+ StreamyUnpacker[BIPoint](data) mustEqual BIPoint(BigInt(50), BigDecimal(25))
+ }
+
+ "object of strings" in {
+ val data = """{"x":"50","y":"25"}"""
+
case class Point(x: Int, y: Int)
- StreamyUnpacker[Point]("""{"x":50,"y":25}""") mustEqual Point(50, 25)
+ StreamyUnpacker[Point](data) mustEqual Point(50, 25)
+
+ case class LPoint(x: Long, y: Int)
+ StreamyUnpacker[LPoint](data) mustEqual LPoint(50L, 25)
+
+ case class BPoint(x: Byte, y: Byte)
+ StreamyUnpacker[BPoint](data) mustEqual BPoint(50.toByte, 25.toByte)
+ case class FPoint(x: Float, y: Double)
+ StreamyUnpacker[FPoint](data) mustEqual FPoint(50.0f, 25.0)
+
+ case class SPoint(x: String, y: Int)
+ StreamyUnpacker[SPoint](data) mustEqual SPoint("50", 25)
+
+ case class BIPoint(x: BigInt, y: BigDecimal)
+ StreamyUnpacker[BIPoint](data) mustEqual BIPoint(BigInt(50), BigDecimal(25))
}
+
}
}

0 comments on commit 1ab697b

Please sign in to comment.