Jackson Scala 2 support that uses scala-reflect to get type info. 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.
This lib is designed to be used with jackson-module-scala. By default, jackson-module-scala uses Java reflection to work out the class structure.
The Scala3 equivalent is jackson-scala3-reflection-extensions.
ScalaReflectExtensions
can be mixed into your ObjectMapper in as a similar way to jackson-module-scala's ClassTagExtensions and ScalaObjectMapper.
libraryDependencies += "com.github.pjfanning" %% "jackson-scala-reflect-extensions" % "2.16.0"
import com.fasterxml.jackson.databind.json.JsonMapper import com.fasterxml.jackson.module.scala.DefaultScalaModule import com.github.pjfanning.jackson.reflect.ScalaReflectExtensions val mapperBuilder = JsonMapper.builder() .addModule(DefaultScalaModule) val mapper = mapperBuilder.build() :: ScalaReflectExtensions // this should also work but Jackson is moving to supporting only creating mapper instances from a builder val mapper2 = new ObjectMapper with ScalaReflectExtensions mapper2.registerModule(DefaultScalaModule) val instance = mapper.readValue[MyClass](jsonText)
If you want the sealed trait support, you will need to register the ScalaReflectAnnotationIntrospectorModule
module.
import com.fasterxml.jackson.databind.json.JsonMapper import com.fasterxml.jackson.module.scala.DefaultScalaModule import com.github.pjfanning.jackson.reflect.ScalaReflectAnnotationIntrospectorModule val mapperBuilder = JsonMapper.builder() .addModule(DefaultScalaModule) .addModule(ScalaReflectAnnotationIntrospectorModule) val mapper = mapperBuilder.build() // this should also work but Jackson is moving to supporting only creating mapper instances from a builder val mapper2 = new ObjectMapper mapper2.registerModule(DefaultScalaModule) mapper2.registerModule(ScalaReflectAnnotationIntrospectorModule)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4