1

I am using: <jackson.version>2.11.2</jackson.version> and <scala.version>2.11.12</scala.version>

pom file:

<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-module-scala_${scala.version.major}</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>

I am having the following error:

Caused by: java.lang.ClassCastException: com.sample.ObjectName$$anon$1 cannot be cast to com.fasterxml.jackson.module.scala.ScalaObjectMapper
at com.fasterxml.jackson.module.scala.ScalaObjectMapper$class.$init$(ScalaObjectMapper.scala:338)
at com.sample.ObjectName$$anon$1.<init>(ObjectName.scala:22)

at this line(updated import statements below for more info):

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
def toMap[V](json:String)(implicit m: Manifest[V]): Map[String, Any] = fromJson[Map[String,Any]](json)
def fromJson[T](json: String)(implicit m : Manifest[T]): T = {
    mapper.readValue[T](json)
}
def toJson(value: Any): String = {
    mapper.writeValueAsString(value)
}

Anyone else faced the same issue? Any suggestions for the same?

user3868051
  • 1,147
  • 2
  • 22
  • 43
  • 2 comments: 1. You dont need to import `jackson-databind`, `jackson-annotations`, and `jackson-core` as they are dependencies of jackson-module-scala. You can see that here: https://mvnrepository.com/artifact/com.fasterxml.jackson.module/jackson-module-scala_2.13/2.11.2 . 2. Can you please share your imports? I don't get the same exception. Which scala version are you using? – Tomer Shetah Sep 10 '20 at 07:37
  • updated scala version and imports above – user3868051 Sep 11 '20 at 15:47
  • Are you still getting the same errors? – Tomer Shetah Sep 11 '20 at 16:10
  • yes, i am running in spark streaming job, its probably some serialization issue, but not sure why or how to fix this in jackson xml – user3868051 Sep 12 '20 at 03:51
  • this solution finally worked to implement serialization this way: https://stackoverflow.com/questions/12591457/scala-2-10-json-serialization-and-deserialization – user3868051 Sep 14 '20 at 16:52

0 Answers0