You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
importcom.fasterxml.jackson.databind.json.JsonMapperimportcom.fasterxml.jackson.module.scala.DefaultScalaModuleimportcom.github.pjfanning.jackson.reflect.ScalaReflectExtensionsvalmapperBuilder=JsonMapper.builder()
.addModule(DefaultScalaModule)
valmapper= mapperBuilder.build() ::ScalaReflectExtensions// this should also work but Jackson is moving to supporting only creating mapper instances from a buildervalmapper2=newObjectMapperwithScalaReflectExtensions
mapper2.registerModule(DefaultScalaModule)
valinstance= mapper.readValue[MyClass](jsonText)
If you want the sealed trait support, you will need to register the ScalaReflectAnnotationIntrospectorModule module.
importcom.fasterxml.jackson.databind.json.JsonMapperimportcom.fasterxml.jackson.module.scala.DefaultScalaModuleimportcom.github.pjfanning.jackson.reflect.ScalaReflectAnnotationIntrospectorModulevalmapperBuilder=JsonMapper.builder()
.addModule(DefaultScalaModule)
.addModule(ScalaReflectAnnotationIntrospectorModule)
valmapper= mapperBuilder.build()
// this should also work but Jackson is moving to supporting only creating mapper instances from a buildervalmapper2=newObjectMapper
mapper2.registerModule(DefaultScalaModule)
mapper2.registerModule(ScalaReflectAnnotationIntrospectorModule)
About
Extension to jackson-module-scala that uses scala-reflect to get type info