jackson-scala3-reflection-extensions
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.
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.15.1"
Scala 3.3+ users should add this dependency:
libraryDependencies += "com.github.pjfanning" %% "scala3-reflection" % "1.3.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)
Known Issues
- Currently does not work with inner classes, (eg case classes defined inside Scala Objects) - see gzoller/scala-reflection#40
Performance
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")