Skip to content

Commit

Permalink
fixed issue debasishg#27 - on deserialization Option[Long] is convert…
Browse files Browse the repository at this point in the history
…ed to Option[BigDecimal]
  • Loading branch information
Debasish Ghosh committed Jul 1, 2011
1 parent 36e0618 commit 71549b3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 13 deletions.
8 changes: 5 additions & 3 deletions src/main/scala/sjson/json/JsBean.scala
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,12 @@ trait JsBean {
if (y.getType.isAssignableFrom(classOf[scala.Option[_]])) {
// handle None case which comes as an empty List since we serialize None as []
if (num.isInstanceOf[List[_]] && num.asInstanceOf[List[_]].isEmpty) y.set(instance, None)
else if (num.isInstanceOf[BigDecimal]) {
val inner = getInnerTypeForOption(y.getType, y)
y.set(instance, Some(mkNum(num, inner)))
}
else y.set(instance, Some(num))
} else {
y.set(instance, num)
}
} else y.set(instance, num)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/sjson/json/Serializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object Serializer {
in[T](Js(json)) // (m)
}

private[json] def in[T: Manifest](js: JsValue): T = {
def in[T: Manifest](js: JsValue): T = {
val m = implicitly[Manifest[T]]
// Map and Tuple2 both are serialized as Maps wrapped within a JsObject
if (m.erasure == classOf[collection.immutable.Map[_, _]] ||
Expand Down
18 changes: 11 additions & 7 deletions src/main/scala/sjson/json/Util.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ object Util {
str.toString
}

def mkNum(v: BigDecimal, c: Class[_]): Any = {
if (c.isAssignableFrom(classOf[Int])) v.asInstanceOf[BigDecimal].intValue
else if (c.isAssignableFrom(classOf[Long])) v.asInstanceOf[BigDecimal].longValue
else if (c.isAssignableFrom(classOf[Float])) v.asInstanceOf[BigDecimal].floatValue
else if (c.isAssignableFrom(classOf[Double])) v.asInstanceOf[BigDecimal].doubleValue
else if (c.isAssignableFrom(classOf[Short])) v.asInstanceOf[BigDecimal].shortValue
else v
def mkNum(v: Any, c: Class[_]) = {
v match {
case b: BigDecimal =>
if (c.isAssignableFrom(classOf[Int])) b.intValue
else if (c.isAssignableFrom(classOf[Long])) b.longValue
else if (c.isAssignableFrom(classOf[Float])) b.floatValue
else if (c.isAssignableFrom(classOf[Double])) b.doubleValue
else if (c.isAssignableFrom(classOf[Short])) b.shortValue
else b
case _ => sys.error("unexpected")
}
}

import java.util.Date
Expand Down
11 changes: 11 additions & 0 deletions src/test/scala/sjson/json/JsonSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,17 @@ class JsonSpec extends Spec with ShouldMatchers {
val o2 = jsBean.fromJSON(Js(json2), Some(classOf[DesignDocument])).asInstanceOf[DesignDocument]
o2.validate_doc_update.isDefined should equal(false)
}

it("should process optional shows correctly") {
val all_pass = "function(newDoc, oldDoc, userCtx) {}" // all valid
val summary = "function(doc, rec) {}"
val detail = "function(doc, rec) {}"
val bar = "function(head, rec) {}"
val zoom = "function(head, rec) {}"
val d1 = DesignDocument("foo_valid", null, Map[String, View](), Some(all_pass), Some(Map("summary" -> summary, "detail" -> detail)), Some(Map("zoom" -> zoom, "bar" -> bar)))
val json1 = jsBean.toJSON(d1)
println(json1)
}
}
}

7 changes: 7 additions & 0 deletions src/test/scala/sjson/json/SerializerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,11 @@ class SerializerSpec extends Spec with ShouldMatchers {
serializer.in[List[List[Address]]](serializer.out(l)) should equal(l)
}
}

describe("Serialization of Long in a bean") {
it("should serialize") {
val m = Manager("alexander", Some(27l))
serializer.in[Manager](serializer.out(m)) should equal(m)
}
}
}
12 changes: 10 additions & 2 deletions src/test/scala/sjson/json/TestBeans.scala
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,20 @@ object TestBeans {
def this() = this (Some("default"), List[String]())
}

@BeanInfo
case class Manager(name: String,
@(OptionTypeHint@field)(value = classOf[Long]) age: Option[Long]) {
private def this() = this ("", None)
}

@BeanInfo
case class DesignDocument(var _id: String,
@(JSONProperty @getter)(ignoreIfNull = true, ignore = false) _rev: String,
@(JSONTypeHint @field)(value = classOf[View]) views: Map[String, View],
@(JSONProperty @getter)(ignoreIfNull = true, ignore = false) validate_doc_update: Option[String] = None) {
private def this() = this(null, null, Map[String, View](), None)
@(JSONProperty @getter)(ignoreIfNull = true, ignore = false) validate_doc_update: Option[String] = None,
@(JSONProperty @getter)(ignoreIfNull = true, ignore = false) shows: Option[Map[String, String]] = None,
@(JSONProperty @getter)(ignoreIfNull = true, ignore = false) lists: Option[Map[String, String]] = None) {
private def this() = this(null, null, Map[String, View]())

override def toString = {
"_id = " + _id + " _rev = " + _rev + " " + " validate = " + validate_doc_update +
Expand Down

0 comments on commit 71549b3

Please sign in to comment.