Found a solution that works with the Jackson and scala classes.
I used the scala module for jackson-jackson-module-scala.
libraryDependencies ++= Seq( "com.fasterxml.jackson.core" % "jackson-databind" % "2.5.3", "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.2.2" )
I needed to annotate the fields in the case class using @JsonProperty.
This class of my case is as follows:
case class Person(@JsonProperty("FName") FName: String, @JsonProperty("LName") LName: String)
And this is how I deserialize:
val objectMapper = new ObjectMapper() with ScalaObjectMapper objectMapper.registerModule(DefaultScalaModule) val str = """{"FName":"Mad", "LName": "Max"}""" val name:Person = objectMapper.readValue[Person](str)
Serialization is easier:
val out = new ByteArrayOutputStream() objectMapper.writeValue(out, name) val json = out.toString
I want to clarify what I use
com.fasterxml.jackson.databind.ObjectMapper
In the question, it seems he uses
org.codehaus.jackson.map.ObjectMapper
which will not work with ScalaObjectMapper.
source share