scalajacksonjson4s

Json4s ignores @JsonProperty jackson annotations


I have this code

import com.fasterxml.jackson.annotation.JsonProperty
import org.json4s.DefaultFormats
import org.json4s.jackson.Serialization.{read, write}

object Testing extends App {
    implicit val formats = DefaultFormats
    val json =
            """
              |{
              |"1strange_field_name":"name"
              |}
            """.stripMargin
    println(read[Test](json))
}

case class Test(@JsonProperty("1strange_field_name") testName: Option[String])

It should be printing Test(Some(name)) but it's printing Test(None). This is caused by the fact that json4s is not using the @JsonProperty annotation. Is there a way to configure json4s to use the jackson annotations?


Solution

  • I've found that the easiest way to solve this is to use the exact field name by using ``

    case class Test(`1strange_field_name`: Option[String])