Skip to content

@JsonProperty is ignored, sometimesΒ #197

@brefsdal

Description

@brefsdal

I've found a case where the JsonProperty annotation is ignored by the serializer. Sometimes. Unfortunately, I haven't found out why this occurs, seemly at random. Here is a example schema

case class MyBase(
  @JsonProperty("my_value"): myValue: Int
)
case class MyClass(
  @JsonProperty("my_name") myName: String,
  @JsonUnwrapped base: MyBase
)
case class App(
  name: String,
  @JsonUnwrapped class: MyClass
)

Most of the time I'll see 'my_name' in the output JSON string. Sometimes a machine will output myName. Once the machine starts outputting camelcase like this, it continues to do so until I restart the JVM. Here is the configuration of the ObjectMapper below

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper, SerializationFeature}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper

class ObjectMapperWithScalaMixin extends ObjectMapper with ScalaObjectMapper
object JacksonConfig {
  val apiObjectMapper = {
    val objectMapper = new ObjectMapperWithScalaMixin
    objectMapper.registerModule(DefaultScalaModule)
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
    objectMapper
  }

JacksonConfig.objectMapper.writeValueAsString(instanceOfApp)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions