I posted this question on SO 
<https://stackoverflow.com/questions/52913897/combine-jackson-mixing-with-valueinstantiator>,
 
but it didn't get much attention, so I'm posting here as well.

-----


I'd like to deserialize the following class:

case class Target(
                   a: Option[Long],
                   b: String
                 )

while providing a custom initial value for missing fields.


I'm using this mixin to overcome this bug: 
https://github.com/FasterXML/jackson-module-scala/wiki/FAQ#deserializing-optionint-and-other-primitive-challenges


trait Mixin {
   @JsonDeserialize(contentAs = classOf[Long]) def a: Option[Long]}


and I'm using this ValueInstantiator to provide a custom initial value:

class TargetInstantiator extends ValueInstantiator{
  override def canCreateUsingDefault: Boolean = true

  override def createUsingDefault(ctxt: DeserializationContext): AnyRef = {
    Target(Some(1), "bbb")
  }}


the mixing and the instantiator work well on their own, but when I combine 
the 2 together, the mixin doesn't seem to have any effect


here is the full code:

case class Target(
                   a: Option[Long],
                   b: String
                 )trait Mixin {
  @JsonDeserialize(contentAs = classOf[Long]) def a: Option[Long]}
class TargetInstantiator extends ValueInstantiator{
  override def canCreateUsingDefault: Boolean = true

  override def createUsingDefault(ctxt: DeserializationContext): AnyRef = {
    Target(Some(1), "bbb")
  }}

val svi = new SimpleValueInstantiators()
svi.addValueInstantiator(classOf[Target], new TargetInstantiator)
val module = new SimpleModule("MyModule")
module.setValueInstantiators(svi)
val mapper = new ObjectMapper()
  .registerModule(DefaultScalaModule)
  .registerModule(module)
  .addMixIn(classOf[Target], classOf[Mixin])val req =
  """{
    |  "a": 123
    |}
  """.stripMarginval res = mapper.readValue(req, classOf[Target])
println(res.a.map(_ + 1)) //should print Some(124)
println(res.b) //should print "bbb"

the above code would fail with the error

java.lang.Integer cannot be cast to java.lang.Long

on the first print.


if I comment out .registerModule(module), and add "b" to the json string, 
the code would work.


Is it possible to make the mixin and the instantiator work together?

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to