@@ -2,11 +2,16 @@ package com.fasterxml.jackson.module.kotlin
2
2
3
3
import com.fasterxml.jackson.databind.MapperFeature
4
4
import com.fasterxml.jackson.databind.module.SimpleModule
5
+ import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullIsSameAsDefault
6
+ import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullToEmptyCollection
7
+ import com.fasterxml.jackson.module.kotlin.KotlinFeature.NullToEmptyMap
8
+ import com.fasterxml.jackson.module.kotlin.KotlinFeature.StrictNullChecks
5
9
import com.fasterxml.jackson.module.kotlin.SingletonSupport.CANONICALIZE
6
10
import com.fasterxml.jackson.module.kotlin.SingletonSupport.DISABLED
11
+ import java.util.*
7
12
import kotlin.reflect.KClass
8
13
9
- private val metadataFqName = " kotlin.Metadata"
14
+ private const val metadataFqName = " kotlin.Metadata"
10
15
11
16
fun Class <* >.isKotlinClass (): Boolean {
12
17
return declaredAnnotations.any { it.annotationClass.java.name == metadataFqName }
@@ -27,7 +32,7 @@ fun Class<*>.isKotlinClass(): Boolean {
27
32
* (e.g. List<String>) may contain null values after deserialization. Enabling it
28
33
* protects against this but has significant performance impact.
29
34
*/
30
- class KotlinModule constructor (
35
+ class KotlinModule @Deprecated(level = DeprecationLevel . WARNING , message = " Use KotlinModule.Builder " ) constructor (
31
36
val reflectionCacheSize : Int = 512 ,
32
37
val nullToEmptyCollection : Boolean = false ,
33
38
val nullToEmptyMap : Boolean = false ,
@@ -37,26 +42,42 @@ class KotlinModule constructor (
37
42
) : SimpleModule(PackageVersion .VERSION ) {
38
43
@Deprecated(level = DeprecationLevel .HIDDEN , message = " For ABI compatibility" )
39
44
constructor (
40
- reflectionCacheSize: Int = 512 ,
41
- nullToEmptyCollection: Boolean = false ,
42
- nullToEmptyMap: Boolean = false
43
- ) : this (reflectionCacheSize, nullToEmptyCollection, nullToEmptyMap, false )
45
+ reflectionCacheSize: Int ,
46
+ nullToEmptyCollection: Boolean ,
47
+ nullToEmptyMap: Boolean
48
+ ) : this (
49
+ Builder ()
50
+ .withReflectionCacheSize(reflectionCacheSize)
51
+ .configure(NullToEmptyCollection , nullToEmptyCollection)
52
+ .configure(NullToEmptyMap , nullToEmptyMap)
53
+ .disable(NullIsSameAsDefault )
54
+ )
44
55
45
56
@Deprecated(level = DeprecationLevel .HIDDEN , message = " For ABI compatibility" )
46
57
constructor (
47
- reflectionCacheSize: Int = 512 ,
48
- nullToEmptyCollection: Boolean = false ,
49
- nullToEmptyMap: Boolean = false ,
50
- nullIsSameAsDefault: Boolean = false
51
- ) : this (reflectionCacheSize, nullToEmptyCollection, nullToEmptyMap, nullIsSameAsDefault)
58
+ reflectionCacheSize: Int ,
59
+ nullToEmptyCollection: Boolean ,
60
+ nullToEmptyMap: Boolean ,
61
+ nullIsSameAsDefault: Boolean
62
+ ) : this (
63
+ Builder ()
64
+ .withReflectionCacheSize(reflectionCacheSize)
65
+ .configure(NullToEmptyCollection , nullToEmptyCollection)
66
+ .configure(NullToEmptyMap , nullToEmptyMap)
67
+ .configure(NullIsSameAsDefault , nullIsSameAsDefault)
68
+ )
52
69
70
+ @Suppress(" DEPRECATION" )
53
71
private constructor (builder: Builder ) : this (
54
72
builder.reflectionCacheSize,
55
- builder.nullToEmptyCollection,
56
- builder.nullToEmptyMap,
57
- builder.nullIsSameAsDefault,
58
- builder.singletonSupport,
59
- builder.strictNullChecks
73
+ builder.isEnabled(NullToEmptyCollection ),
74
+ builder.isEnabled(NullToEmptyMap ),
75
+ builder.isEnabled(NullIsSameAsDefault ),
76
+ when {
77
+ builder.isEnabled(KotlinFeature .SingletonSupport ) -> CANONICALIZE
78
+ else -> DISABLED
79
+ },
80
+ builder.isEnabled(StrictNullChecks )
60
81
)
61
82
62
83
companion object {
@@ -76,7 +97,7 @@ class KotlinModule constructor (
76
97
77
98
context.addValueInstantiators(KotlinInstantiators (cache, nullToEmptyCollection, nullToEmptyMap, nullIsSameAsDefault, strictNullChecks))
78
99
79
- when (singletonSupport) {
100
+ when (singletonSupport) {
80
101
DISABLED -> Unit
81
102
CANONICALIZE -> {
82
103
context.addBeanDeserializerModifier(KotlinBeanDeserializerModifier )
@@ -104,38 +125,104 @@ class KotlinModule constructor (
104
125
var reflectionCacheSize: Int = 512
105
126
private set
106
127
107
- var nullToEmptyCollection: Boolean = false
108
- private set
128
+ private val bitSet: BitSet = 0 .toBitSet().apply {
129
+ KotlinFeature .values().filter { it.enabledByDefault }.forEach { or (it.bitSet) }
130
+ }
109
131
110
- var nullToEmptyMap: Boolean = false
111
- private set
132
+ fun withReflectionCacheSize (reflectionCacheSize : Int ) = apply {
133
+ this .reflectionCacheSize = reflectionCacheSize
134
+ }
112
135
113
- var nullIsSameAsDefault: Boolean = false
114
- private set
136
+ fun enable (feature : KotlinFeature ) = apply {
137
+ bitSet.or (feature.bitSet)
138
+ }
115
139
116
- var singletonSupport = DISABLED
117
- private set
140
+ fun disable (feature : KotlinFeature ) = apply {
141
+ bitSet.andNot(feature.bitSet)
142
+ }
118
143
119
- var strictNullChecks = false
120
- private set
144
+ fun configure (feature : KotlinFeature , enabled : Boolean ) = when {
145
+ enabled -> enable(feature)
146
+ else -> disable(feature)
147
+ }
121
148
122
- fun reflectionCacheSize ( reflectionCacheSize : Int ) = apply { this .reflectionCacheSize = reflectionCacheSize }
149
+ fun isEnabled ( feature : KotlinFeature ): Boolean = bitSet.intersects(feature.bitSet)
123
150
124
- fun nullToEmptyCollection (nullToEmptyCollection : Boolean ) =
125
- apply { this .nullToEmptyCollection = nullToEmptyCollection }
151
+ @Deprecated(
152
+ message = " Deprecated, use withReflectionCacheSize(reflectionCacheSize) instead." ,
153
+ replaceWith = ReplaceWith (" isEnabled(reflectionCacheSize)" )
154
+ )
155
+ fun reflectionCacheSize (reflectionCacheSize : Int ) = apply {
156
+ this .reflectionCacheSize = reflectionCacheSize
157
+ }
158
+
159
+ @Deprecated(
160
+ message = " Deprecated, use isEnabled(NullToEmptyCollection) instead." ,
161
+ replaceWith = ReplaceWith (" isEnabled(NullToEmptyCollection)" )
162
+ )
163
+ fun getNullToEmptyCollection () = isEnabled(NullToEmptyCollection )
126
164
127
- fun nullToEmptyMap (nullToEmptyMap : Boolean ) = apply { this .nullToEmptyMap = nullToEmptyMap }
165
+ @Deprecated(
166
+ message = " Deprecated, use configure(NullToEmptyCollection, enabled) instead." ,
167
+ replaceWith = ReplaceWith (" configure(NullToEmptyCollection, enabled)" )
168
+ )
169
+ fun nullToEmptyCollection (nullToEmptyCollection : Boolean ) =
170
+ configure(NullToEmptyCollection , nullToEmptyCollection)
171
+
172
+ @Deprecated(
173
+ message = " Deprecated, use isEnabled(NullToEmptyMap) instead." ,
174
+ replaceWith = ReplaceWith (" isEnabled(NullToEmptyMap)" )
175
+ )
176
+ fun getNullToEmptyMap () = isEnabled(NullToEmptyMap )
177
+
178
+ @Deprecated(
179
+ message = " Deprecated, use configure(NullToEmptyMap, enabled) instead." ,
180
+ replaceWith = ReplaceWith (" configure(NullToEmptyMap, enabled)" )
181
+ )
182
+ fun nullToEmptyMap (nullToEmptyMap : Boolean ) = configure(NullToEmptyMap , nullToEmptyMap)
183
+
184
+ @Deprecated(
185
+ message = " Deprecated, use isEnabled(NullIsSameAsDefault) instead." ,
186
+ replaceWith = ReplaceWith (" isEnabled(NullIsSameAsDefault)" )
187
+ )
188
+ fun getNullIsSameAsDefault () = isEnabled(NullIsSameAsDefault )
189
+
190
+ @Deprecated(
191
+ message = " Deprecated, use configure(NullIsSameAsDefault, enabled) instead." ,
192
+ replaceWith = ReplaceWith (" configure(NullIsSameAsDefault, enabled)" )
193
+ )
194
+ fun nullIsSameAsDefault (nullIsSameAsDefault : Boolean ) = configure(NullIsSameAsDefault , nullIsSameAsDefault)
195
+
196
+ @Deprecated(
197
+ message = " Deprecated, use isEnabled(SingletonSupport) instead." ,
198
+ replaceWith = ReplaceWith (" isEnabled(SingletonSupport)" )
199
+ )
200
+ fun getSingletonSupport () = when {
201
+ isEnabled(KotlinFeature .SingletonSupport ) -> CANONICALIZE
202
+ else -> DISABLED
203
+ }
128
204
129
- fun nullIsSameAsDefault (nullIsSameAsDefault : Boolean ) = apply { this .nullIsSameAsDefault = nullIsSameAsDefault }
205
+ @Deprecated(
206
+ message = " Deprecated, use configure(SingletonSupport, enabled) instead." ,
207
+ replaceWith = ReplaceWith (" configure(SingletonSupport, enabled)" )
208
+ )
209
+ fun singletonSupport (singletonSupport : SingletonSupport ) = when (singletonSupport) {
210
+ CANONICALIZE -> enable(KotlinFeature .SingletonSupport )
211
+ else -> disable(KotlinFeature .SingletonSupport )
212
+ }
130
213
131
- fun singletonSupport (singletonSupport : SingletonSupport ) =
132
- apply { this .singletonSupport = singletonSupport }
214
+ @Deprecated(
215
+ message = " Deprecated, use isEnabled(StrictNullChecks) instead." ,
216
+ replaceWith = ReplaceWith (" isEnabled(StrictNullChecks)" )
217
+ )
218
+ fun getStrictNullChecks () = isEnabled(StrictNullChecks )
133
219
134
- fun strictNullChecks (strictNullChecks : Boolean ) =
135
- apply { this .strictNullChecks = strictNullChecks }
220
+ @Deprecated(
221
+ message = " Deprecated, use configure(StrictNullChecks, enabled) instead." ,
222
+ replaceWith = ReplaceWith (" configure(StrictNullChecks, enabled)" )
223
+ )
224
+ fun strictNullChecks (strictNullChecks : Boolean ) = configure(StrictNullChecks , strictNullChecks)
136
225
137
226
fun build () = KotlinModule (this )
138
227
}
139
228
}
140
-
141
-
0 commit comments