2

I am upgrading my version of Scala to 2.12 from 2.11 and the object mapper seems to break. Other parts of my code require features only available under 2.12.

This using scala 2.12 with spark 2.1 mentions rebuilding Jackson as a possible solution. Is this truely necessary or is there a simpler solution?

SBT configuration for Scala 2.11.0

// Identity
name := "ScalaJsonSpike00"
organization := "com.acme"

// Versions
version := "1.0"
scalaVersion := "2.11.0"

// Scala test
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.0" % "test"

// JSON
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
libraryDependencies += "com.fasterxml.jackson.core" % "jackson-databind" % "2.8.8"
// https://mvnrepository.com/artifact/com.fasterxml.jackson.module/jackson-module-scala_2.11
libraryDependencies += "com.fasterxml.jackson.module" % "jackson-module-scala_2.11" % "2.8.8"

Code for both 2.11 and 2.12

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper

object Main {
  def main(args: Array[String]): Unit = {

    val originalMap = Map("a" -> List(1,2), "b" -> List(3,4,5), "c" -> List())
    val mapper = new ObjectMapper() with ScalaObjectMapper
    mapper.registerModule(DefaultScalaModule)
    println(mapper.writeValueAsString(originalMap))
  }
}

Result with Scala 2.11.0

{"a":[1,2],"b":[3,4,5],"c":[]}

SBT configuration update to scala 2.12.1

scalaVersion := "2.12.1"

Result with Scala 2.12.1

com.intellij.rt.execution.application.AppMain Main
Exception in thread "main" java.lang.NoSuchMethodError: com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper.$init$(Lcom/fasterxml/jackson/module/scala/experimental/ScalaObjectMapper;)V
    at Main$$anon$1.<init>(Main.scala:9)
    at Main$.main(Main.scala:9)
    at Main.main(Main.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Community
  • 1
  • 1
s.k
  • 519
  • 4
  • 7
  • 23
  • I cannot find any mention of rebuilding Jackson in **using scala 2.12 with spark 2.1**. Did you really mean to link this particular question? – Suma Apr 23 '17 at 06:58
  • Sorry you are right. I was referring to this comment 'yea but how can it work if scala 2.11 is not binary compatible with 2.12?' and had miss-applied it to Jackson. Very tired after much searching. – s.k Apr 23 '17 at 09:08

2 Answers2

6

You should use %% to get jackson-module-scala: appropriate for your Scala version:

libraryDependencies += "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.8.8"

In your current version you always use Scala 2.11 version of this module, which is not binary compatible with Scala 2.12.

There is no such issue with core Jackson libraries, as those as Java ones, and therefore not affected by Scala version in any way.

Suma
  • 33,181
  • 16
  • 123
  • 191
  • Thank you I updated the configuration to the following `libraryDependencies += "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.8.8"` Which worked for my test case. – s.k Apr 23 '17 at 09:15
  • Awesome, saved my day! – Uma Feb 16 '21 at 15:48
  • 2
    Amusing update: If you are trying to get this to work within Play 2.8.8 then the current answer is: ```"com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.12.3"``` See: https://stackoverflow.com/questions/68009991/play-framework-2-8-8-with-aws-java-sdk-1-12-6-com-fasterxml-jackson-databind-js – Techmag Jun 17 '21 at 13:21
0

The solution i found works is to copy all the code from ScalaObjectMapper into a scala class that extends Object Mapper, and then instantiate that.

G. Potok
  • 42
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 23 '22 at 22:07