Skip to content
This repository has been archived by the owner on Jan 15, 2022. It is now read-only.

Commit

Permalink
Addresses salat#99 - salat will coerce incoming ints to doubles when …
Browse files Browse the repository at this point in the history
…deserializing DBO and JSON.
  • Loading branch information
rktoomey committed Sep 17, 2013
1 parent 9926f6e commit b738414
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion project/SalatBuild.scala
Expand Up @@ -149,7 +149,7 @@ object Dependencies {
private val LogbackVersion = "1.0.9"
private val CasbahVersion = "2.6.2"

val specs2 = "org.specs2" %% "specs2" % "2.2-SNAPSHOT" % "test"
val specs2 = "org.specs2" %% "specs2" % "2.2" % "test"
val commonsLang = "commons-lang" % "commons-lang" % "2.6" % "test"
val slf4jApi = "org.slf4j" % "slf4j-api" % "1.7.2"
val logbackCore = "ch.qos.logback" % "logback-core" % LogbackVersion % "test"
Expand Down
Expand Up @@ -202,6 +202,9 @@ package out {
trait FloatToDouble extends Transformer {
self: Transformer =>
override def transform(value: Any)(implicit ctx: Context) = value match {
case i: Int => i.toDouble
case l: Long => l.toDouble
case s: Short => s.toDouble
case f: Float => f.toDouble
case f: java.lang.Float => f.doubleValue()
}
Expand Down
14 changes: 14 additions & 0 deletions salat-core/src/test/scala/com/novus/salat/test/DoubleSpec.scala
@@ -0,0 +1,14 @@
package com.novus.salat.test

import com.novus.salat._
import com.novus.salat.test.global._
import com.novus.salat.test.model._
import com.mongodb.casbah.Imports._

class DoubleSpec extends SalatSpec {
"Salat" should {
"deserialize an int to a double and an option double" in {
grater[DoubleTest].asObject(DBObject("d" -> 9, "d2" -> 9)) must_== DoubleTest(9d, Option(9d))
}
}
}
Expand Up @@ -90,3 +90,4 @@ case class Petter(d: Option[DateTime])
case class Qvintus(bd: Option[BigDecimal])
case class Rudolf(bi: Option[BigInt])
case class Sigurd(o: Option[ObjectId])
case class Tore(i: Int, d: Double)
Expand Up @@ -224,6 +224,11 @@ class JsonSpec extends Specification with Logging with JsonMatchers {
Nil)
grater[Adam].fromJSON(j) must_== a
}
"be flexible about coercing Double <-> Int when deserializing" in {
val t = Tore(i = 9, d = 9.0)
grater[Tore].fromJSON("{\"i\":9.0,\"d\":9}") must_== t
grater[Tore].fromJSON(JObject(JField("i", JDouble(9d)) :: JField("d", JInt(9)) :: Nil)) must_== t
}
"deserialize JObjects containing DateTimes" in {
val olof = Olof(date)
val petter = Petter(Some(date))
Expand Down
Expand Up @@ -205,6 +205,8 @@ case class Susan2(how: String = "who", perished: Boolean = true, fits: List[Fit]
sealed trait Una
case object SlippedDownADrain extends Una

case class DoubleTest(d: Double, d2: Option[Double])

case class Employee(name: String, age: Option[Int], annual_salary: Option[ScalaBigDecimal])
case class Department(name: String, head_honcho: Option[Employee], cya_factor: ScalaBigDecimal, minions: List[Employee])
case class Company(name: String, year_of_inception: Int, departments: Map[String, Department])
Expand Down

0 comments on commit b738414

Please sign in to comment.