Jackson Scala 3 support that uses scala3-reflection to get type info based on Scala 3 Tasty files (or at compile time, see Performance section).
The problem that this lib solves in described in this FAQ entry.
The lib can also auto-discover subtypes if you are using Jackson's polymorphism support (@JsonTypeInfo annotation). You can omit the @JsonSubTypes
if you dealing with sealed traits.
See jackson-scala-reflect-extensions for the Scala 2 equivalent.
This lib is designed to used with jackson-module-scala. By default, jackson-module-scala uses Java reflection to work out the class structure.
ScalaReflectionExtensions
can be mixed into your ObjectMapper in as a similar way to jackson-module-scala's
ClassTagExtensions
and ScalaObjectMapper.
libraryDependencies += "com.github.pjfanning" %% "jackson-scala3-reflection-extensions" % "2.16.0"
import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.github.pjfanning.jackson.reflection.ScalaReflectionExtensions
val mapperBuilder = JsonMapper.builder()
.addModule(DefaultScalaModule)
val mapper = mapperBuilder.build() :: ScalaReflectionExtensions
// this should also work but Jackson is moving to supporting only creating mapper instances from a builder
val mapper2 = new ObjectMapper with ScalaReflectionExtensions
mapper2.registerModule(DefaultScalaModule)
val instance = mapper.readValue[MyClass](jsonText)
The code to calculate the class details can be slow, as detailed in gzoller/scala-reflection.
The results are cached, so they won't be recalculated every time you call readValue
.
If performance worries you then you should consider enabling the compiler plugin.
addCompilerPlugin("com.github.pjfanning" %% "scala3-reflection")