Hi,
While mapping an object from a JSON string (using "jackson-module-scala_2.10" % "2.3.1"), how can I make Jackson to thrown an exception for the missing fields? That is, consider the following code snippet.
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.scalatest.FunSuite
class TickerResponse
(val low: Double,
val high: Double,
val last: Double,
val bid: Double,
val ask: Double,
val volume: Double,
val timestamp: Double)
class ResponseSuite extends FunSuite {
private val mapper = new ObjectMapper()
.registerModule(DefaultScalaModule)
.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true)
.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, true)
.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, true)
test("TickerResponse deserialization should fail on bogus data") {
val json = """{"high":1418.73, "last":1402.28}"""
val res = mapper.readValue(json, classOf[TickerResponse])
println(f"res.ask: ${res.ask}")
}
}
Here, the test outputs
"res.ask: 0.0", whereas,
readValue() should have thrown an exception. Any ideas?
Best.