1
1
package com .fasterxml .jackson .module .scala .introspect
2
2
3
- import java .lang .annotation .Annotation
4
3
import com .fasterxml .jackson .annotation .JsonCreator
5
4
import com .fasterxml .jackson .databind .`type` .ClassKey
6
5
import com .fasterxml .jackson .databind .cfg .MapperConfig
7
6
import com .fasterxml .jackson .databind .deser .std .StdValueInstantiator
8
7
import com .fasterxml .jackson .databind .deser ._
9
8
import com .fasterxml .jackson .databind .introspect ._
10
9
import com .fasterxml .jackson .databind .util .{AccessPattern , LRUMap , LookupCache }
11
- import com .fasterxml .jackson .databind .{BeanDescription , DeserializationConfig , DeserializationContext , DeserializationFeature , MapperFeature }
10
+ import com .fasterxml .jackson .databind .{BeanDescription , DeserializationConfig , DeserializationContext , MapperFeature }
12
11
import com .fasterxml .jackson .module .scala .JacksonModule
13
12
import com .fasterxml .jackson .module .scala .util .Implicits ._
14
13
14
+ import java .lang .annotation .Annotation
15
+ import scala .collection .JavaConverters ._
16
+
15
17
object ScalaAnnotationIntrospector extends NopAnnotationIntrospector with ValueInstantiators {
16
18
private [this ] var _descriptorCache : LookupCache [ClassKey , BeanDescriptor ] =
17
19
new LRUMap [ClassKey , BeanDescriptor ](16 , 100 )
18
20
19
- private case class FieldKey ( clazz : Class [_], fieldName : String )
21
+ private case class ClassOverrides ( overrides : scala.collection.mutable. Map [ String , Class [_]] = scala.collection.mutable. Map .empty )
20
22
21
- private val overrideMap = scala.collection.mutable.Map [FieldKey , Class [_]]()
23
+ private val overrideMap = scala.collection.mutable.Map [Class [_], ClassOverrides ]()
22
24
23
25
/**
24
26
* jackson-module-scala does not always properly handle deserialization of Options or Collections wrapping
@@ -31,7 +33,7 @@ object ScalaAnnotationIntrospector extends NopAnnotationIntrospector with ValueI
31
33
* @param referencedType
32
34
*/
33
35
def registerReferencedType (clazz : Class [_], fieldName : String , referencedType : Class [_]): Unit = {
34
- overrideMap.update( FieldKey ( clazz, fieldName) , referencedType)
36
+ overrideMap.getOrElseUpdate( clazz, ClassOverrides ()).overrides.update(fieldName , referencedType)
35
37
}
36
38
37
39
def clearRegisteredReferencedTypes (): Unit = {
@@ -128,15 +130,17 @@ object ScalaAnnotationIntrospector extends NopAnnotationIntrospector with ValueI
128
130
extends StdValueInstantiator (delegate) {
129
131
130
132
private val overriddenConstructorArguments : Array [SettableBeanProperty ] = {
133
+ val overrides = overrideMap.get(descriptor.beanType).map(_.overrides.toMap).getOrElse(Map .empty)
131
134
val applyDefaultValues = config.isEnabled(MapperFeature .APPLY_DEFAULT_VALUES )
132
135
val args = delegate.getFromObjectArguments(config)
133
136
Option (args) match {
134
- case Some (array) if (applyDefaultValues || overrideMap .nonEmpty) => {
137
+ case Some (array) if (applyDefaultValues || overrides .nonEmpty) => {
135
138
array.map {
136
139
case creator : CreatorProperty => {
137
140
// Locate the constructor param that matches it
138
141
descriptor.properties.find(_.param.exists(_.index == creator.getCreatorIndex)) match {
139
142
case Some (pd) => {
143
+ println(s " >>>>> override ${pd.name} ${overrides.get(pd.name)}" )
140
144
if (applyDefaultValues) {
141
145
pd match {
142
146
case PropertyDescriptor (_, Some (ConstructorParameter (_, _, Some (defaultValue))), _, _, _, _, _) => {
@@ -173,7 +177,7 @@ object ScalaAnnotationIntrospector extends NopAnnotationIntrospector with ValueI
173
177
if (isMaybeScalaBeanType(beanDesc.getBeanClass)) {
174
178
175
179
_descriptorFor(beanDesc.getBeanClass).map { descriptor =>
176
- if (descriptor.properties.exists(_.param.exists(_.defaultValue.isDefined))) {
180
+ if (overrideMap.contains(beanDesc.getBeanClass) || descriptor.properties.exists(_.param.exists(_.defaultValue.isDefined))) {
177
181
defaultInstantiator match {
178
182
case std : StdValueInstantiator =>
179
183
new ScalaValueInstantiator (std, config, descriptor)
0 commit comments