Marshall/Unmarshalling Scala Case class with Jackson Jsonformat not working

huangapple go评论56阅读模式
英文:

Marshall/Unmarshalling Scala Case class with Jackson Jsonformat not working

问题

gradle配置

'com.fasterxml.jackson.core:jackson-databind:2.9.5',
"com.fasterxml.jackson.module:jackson-module-scala_2.12:2.9.5",
'com.fasterxml.jackson.datatype:jackson-datatype-joda:2.9.5',

案例类如下所示

case class SampleCaseClass(@JsonProperty("a") @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ") a: DateTime,
                                    @JsonProperty("b_id") b: String,
                                    @JsonProperty("c_id") c: String) {

}

实例化

  val sampleCaseClass =
            SampleCaseClass(
                new DateTime(10L),
                "abc",
                "efg",
            )

将案例类转换为JSON

  val mapper = new ObjectMapper() with ScalaObjectMapper
  mapper.registerModule(DefaultScalaModule)
  mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
  mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
  val inputJson = new StringReader(mapper.writeValueAsString(sampleCaseClass))

日期值如下

{"year":1970,"dayOfMonth":1,"dayOfWeek":4,"dayOfYear":1,"era":1,"millisOfDay":3600010,"weekOfWeekyear":1,"weekyear":1970,"monthOfYear":1,"yearOfEra":1970,"yearOfCentury":70,"centuryOfEra":19,"millisOfSecond":10,"secondOfMinute":0,"secondOfDay":3600,"minuteOfHour":0,"minuteOfDay":60,"hourOfDay":1,"chronology":{"zone":{"fixed":false,"uncachedZone":{"fixed":false,"cachable":true,"id":"Europe/Berlin"},"id":"Europe/Berlin"}},"zone":{"fixed":false,"uncachedZone":{"fixed":false,"cachable":true,"id":"Europe/Berlin"},"id":"Europe/Berlin"},"millis":10,"beforeNow":true,"afterNow":false,"equalNow":false}

由于某些原因,JsonFormat不起作用,而JsonProperty起作用。

如何确保日期格式正确。我希望在案例类级别进行此操作,因为在将JSON转换回对象时,我不希望遇到任何问题。我正在尝试将值插入Elasticsearch。我使用的是springboot 2.7.6,内部使用的是7.17 Elasticsearch版本。我尝试使用新的JAVA API客户端插入值。

英文:

gradle configurations

            'com.fasterxml.jackson.core:jackson-databind:2.9.5',
            "com.fasterxml.jackson.module:jackson-module-scala_2.12:2.9.5",
            'com.fasterxml.jackson.datatype:jackson-datatype-joda:2.9.5',

case class looks like this

case class SampleCaseClass(@JsonProperty("a") @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ") a: DateTime,
                                    @JsonProperty("b_id") b: String,
                                    @JsonProperty("c_id") c: String) {

}

instantiation

  val sampleCaseClass =
            SampleCaseClass(
                new DateTime(10L),
                "abc",
                "efg",
            )

converting case class to json

      val mapper = new ObjectMapper() with ScalaObjectMapper
      mapper.registerModule(DefaultScalaModule)
      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
      mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
      val inputJson = new StringReader(mapper.writeValueAsString(sampleCaseClass))

Date value is coming

{"year":1970,"dayOfMonth":1,"dayOfWeek":4,"dayOfYear":1,"era":1,"millisOfDay":3600010,"weekOfWeekyear":1,"weekyear":1970,"monthOfYear":1,"yearOfEra":1970,"yearOfCentury":70,"centuryOfEra":19,"millisOfSecond":10,"secondOfMinute":0,"secondOfDay":3600,"minuteOfHour":0,"minuteOfDay":60,"hourOfDay":1,"chronology":{"zone":{"fixed":false,"uncachedZone":{"fixed":false,"cachable":true,"id":"Europe/Berlin"},"id":"Europe/Berlin"}},"zone":{"fixed":false,"uncachedZone":{"fixed":false,"cachable":true,"id":"Europe/Berlin"},"id":"Europe/Berlin"},"millis":10,"beforeNow":true,"afterNow":false,"equalNow":false}

JsonFormat for some reasons not working. where as JsonProperty works

how to make sure that date is properly formatted. I want to do this at case class level, because while converting json back to object I don't want to face any issues. Am trying to insert values into Elasticsearch. I am using springboot 2.7.6, internally it uses 7.17 Elasticsearch version. I am trying to use new JAVA API client to insert the values.

答案1

得分: 1

maybe the issue 如何解决 case class Option[LocalDate] ? #332 可能有助于解决您的问题。看起来您需要为 ObjectMapper 注册 JavaTimeModule

new ObjectMapper().registerModule(new JavaTimeModule).registerModule(DefaultScalaModule)

还需要将 jackson-modules-java8 添加为依赖项。这个库支持 JSR-310 规范(Java 8 日期/时间类型)。

如果我没记错的话,根据您列出的依赖关系,您正在使用 joda time。在这种情况下,您需要为来自 jackson-datatype-jodaObjectMapper 注册 JodaModule,这个依赖已经导入。

英文:

maybe the issue How to resolve case class Option[LocalDate] ? #332 could help to solve your problem. Looks like you need to register the JavaTimeModule for the ObjectMapper

new ObjectMapper().registerModule(new JavaTimeModule).registerModule(DefaultScalaModule)

also you need to add jackson-modules-java8 as dependency. This one has support for JSR-310 specification (Java 8 date/time types).

If I'm not wrong, based on the dependencies you listed, you are using joda time. In thath case you would need to register the JodaModule for the ObjectMapper that comes from jackson-datatype-joda, the dependency that is already imported

huangapple
  • 本文由 发表于 2023年5月26日 11:50:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定