Home > Software engineering >  Scala jackson - custom deserializer incompatible type
Scala jackson - custom deserializer incompatible type

Time:04-02

I have been using Jackson succesfully to serialize/deserialize my scala objects, but I am having trouble adding a custom deserializer to one of my properties.

 case class Test(
     @JsonDeserialize(using = classOf[BooleanKeyDeserializer]) boolValue: Option[Boolean]
 )
import com.fasterxml.jackson.core.{JsonParser, JsonProcessingException}
import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer}

case class BooleanKeyDeserializer() extends JsonDeserializer {
  @throws[IOException]
  @throws[JsonProcessingException]
  override def deserialize(p: JsonParser, ctxt: DeserializationContext): Boolean = {
    true
  }
}

Error: overriding method deserialize in class JsonDeserializer of type (x$1: com.fasterxml.jackson.core.JsonParser, x$2: com.fasterxml.jackson.databind.DeserializationContext)Nothing; method deserialize has incompatible type

I think it may be the Nothing here that is throwing an error but I am not sure since the overridden method returns a Boolean, any ideas?

CodePudding user response:

I'm not much familiar with Jackson, but just had a quick look on github for some examples, and found out that you should let your JsonDeserializer class know which type it is supposed to deserialize at compile time, so:

case class BooleanKeyDeserializer() extends JsonDeserializer[Boolean]

would probably suit you fine.

  • Related