jackson-module-scala icon indicating copy to clipboard operation
jackson-module-scala copied to clipboard

Integer runtime value in a collection typed as Double

Open l15k4 opened this issue 9 years ago • 4 comments

Easily reproducible :

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.core.util.{DefaultPrettyPrinter, MinimalPrettyPrinter}
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper

object ObjMapper extends ObjectMapper with ScalaObjectMapper {
  setSerializationInclusion(JsonInclude.Include.NON_NULL)
  registerModule(DefaultScalaModule)
  val prettyWriter = writer(new DefaultPrettyPrinter)
  val miniWriter = writer(new MinimalPrettyPrinter)
}

case class Foo(timestamp: String, result: Map[String, Double])

class PLAY extends BaseSuite {

  "succeeds" in {
    val json =
      """
        |[ {
        |    "count" : 10800
        |  }
        |]
      """.stripMargin

    val map = ObjMapper.readValue[List[Map[String,Double]]](json).head
    assert(map("count").isInstanceOf[Double])
  }


  "fails" in {
    val json =
      """
        |{
        |  "timestamp" : "2015-01-01T00:00:00.000Z",
        |  "result" : {
        |    "count" : 10800
        |  }
        |}
      """.stripMargin

    val map = ObjMapper.readValue[Foo](json).result
    assert(map("count").isInstanceOf[Double])
  }

}

l15k4 avatar Jul 20 '16 10:07 l15k4

Is this issue scheduled to be fixed?

the-ricker avatar Dec 07 '16 13:12 the-ricker

I've also run into this one - might take a crack at resolving it when I have the time.

Diagoras avatar Jan 20 '17 15:01 Diagoras

This is covered in the FAQ

tewe avatar Oct 16 '20 09:10 tewe

https://github.com/pjfanning/jackson-scala-reflect-extensions may be an option for people running in this issue.

pjfanning avatar Aug 08 '22 15:08 pjfanning