jackson-module-scala-duration

Build Status Maven Central

This module needs to be used along with jackson-datatype-jsr310 JavaTimeModule. What this module does is to convert Scala FiniteDurations into Java Time Durations and vice versa. JavaTimeModule then does the rest.

This module supports Scala 2.11, 2.12, 2.13 and 3.

If you need to support Scala classes generally, you will also need to add jackson-module-scala.

If you don't use this module, Scala FiniteDurations can still be serialized but they will appear in a format like:

{"length":7,"unit":"DAYS","finite":true}

The format seems to differ depending on Scala version (because the internals of the FiniteDuration class can change from release to release). Deserialization seems to be problematic regardless of Scala version.

When this module is used, the serialization format defaults to writing durations as numbers. The format can be changed by enabling/disabling these jackson-datatype-jsr310 features:

  • SerializationFeature.WRITE_DATES_AS_TIMESTAMPS
    • if you disable this, the format used is ISO-8601 format (eg 'PT12H' for 12 hours)
  • SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS appears to have no effect on durations
  • DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS appears to have no effect on durations
val jacksonVersion = "2.13.3"

libraryDependencies ++= Seq(
  "com.fasterxml.jackson.datatype" % "jackson-datatype-jsr310" % jacksonVersion,
  "com.fasterxml.jackson.module" %% "jackson-module-scala" % jacksonVersion,
  "com.github.pjfanning" %% "jackson-module-scala-duration" % jacksonVersion
)
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.github.pjfanning.scala.duration.DurationModule

val mapper = JsonMapper.builder()
    .addModule(new JavaTimeModule)
    .addModule(DefaultScalaModule)
    .addModule(DurationModule)
    .build()