-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathExtensions.kt
More file actions
318 lines (288 loc) · 11.8 KB
/
Extensions.kt
File metadata and controls
318 lines (288 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package io.github.projectmapk.jackson.module.kogera
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.TreeNode
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.JsonSerializer
import com.fasterxml.jackson.databind.MappingIterator
import com.fasterxml.jackson.databind.Module
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.ObjectReader
import com.fasterxml.jackson.databind.RuntimeJsonMappingException
import com.fasterxml.jackson.databind.cfg.MapperBuilder
import com.fasterxml.jackson.databind.cfg.MutableConfigOverride
import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.node.ArrayNode
import com.fasterxml.jackson.databind.node.ObjectNode
import java.io.File
import java.io.InputStream
import java.io.Reader
import java.math.BigDecimal
import java.math.BigInteger
import java.net.URL
import kotlin.reflect.KClass
public fun kotlinModule(initializer: KotlinModule.Builder.() -> Unit = {}): KotlinModule {
val builder = KotlinModule.Builder()
builder.initializer()
return builder.build()
}
public fun jsonMapper(initializer: JsonMapper.Builder.() -> Unit = {}): JsonMapper {
val builder = JsonMapper.builder()
builder.initializer()
return builder.build()
}
// region: JvmOverloads is set for bytecode compatibility for versions below 2.17.
@JvmOverloads
public fun jacksonObjectMapper(initializer: KotlinModule.Builder.() -> Unit = {}): ObjectMapper = jsonMapper {
addModule(kotlinModule(initializer))
}
@JvmOverloads
public fun jacksonMapperBuilder(initializer: KotlinModule.Builder.() -> Unit = {}): JsonMapper.Builder = JsonMapper
.builder()
.addModule(kotlinModule(initializer))
@JvmOverloads
public fun ObjectMapper.registerKotlinModule(
initializer: KotlinModule.Builder.() -> Unit = {},
): ObjectMapper = this.registerModule(kotlinModule(initializer))
// endregion
public inline fun <reified T> jacksonTypeRef(): TypeReference<T> = object : TypeReference<T>() {}
@PublishedApi
internal inline fun <reified T> Any?.checkTypeMismatch(): T {
// Basically, this check assumes that T is non-null and the value is null.
// Since this can be caused by both input or ObjectMapper implementation errors,
// a more abstract RuntimeJsonMappingException is thrown.
if (this !is T) {
val nullability = if (null is T) "?" else "(non-null)"
// Since the databind implementation of MappingIterator throws RuntimeJsonMappingException,
// JsonMappingException was not used to unify the behavior.
throw RuntimeJsonMappingException(
"Deserialized value did not match the specified type; " +
"specified ${T::class.qualifiedName}$nullability but was ${this?.let { it::class.qualifiedName }}",
)
}
return this
}
/**
* Shorthand for [ObjectMapper.readValue].
* @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null.
* Other cases where the read value is of a different type than [T]
* due to an incorrect customization to [ObjectMapper].
*/
public inline fun <reified T> ObjectMapper.readValue(jp: JsonParser): T = readValue(jp, jacksonTypeRef<T>())
.checkTypeMismatch()
/**
* Shorthand for [ObjectMapper.readValues].
* @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null.
* Other cases where the read value is of a different type than [T]
* due to an incorrect customization to [ObjectMapper].
*/
public inline fun <reified T> ObjectMapper.readValues(jp: JsonParser): MappingIterator<T> {
val values = readValues(jp, jacksonTypeRef<T>())
return object : MappingIterator<T>(values) {
override fun nextValue(): T = super.nextValue().checkTypeMismatch()
}
}
/**
* Shorthand for [ObjectMapper.readValue].
* @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null.
* Other cases where the read value is of a different type than [T]
* due to an incorrect customization to [ObjectMapper].
*/
public inline fun <reified T> ObjectMapper.readValue(src: File): T = readValue(src, jacksonTypeRef<T>())
.checkTypeMismatch()
/**
* Shorthand for [ObjectMapper.readValue].
* @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null.
* Other cases where the read value is of a different type than [T]
* due to an incorrect customization to [ObjectMapper].
*/
public inline fun <reified T> ObjectMapper.readValue(src: URL): T = readValue(src, jacksonTypeRef<T>())
.checkTypeMismatch()
/**
* Shorthand for [ObjectMapper.readValue].
* @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null.
* Other cases where the read value is of a different type than [T]
* due to an incorrect customization to [ObjectMapper].
*/
public inline fun <reified T> ObjectMapper.readValue(content: String): T = readValue(content, jacksonTypeRef<T>())
.checkTypeMismatch()
/**
* Shorthand for [ObjectMapper.readValue].
* @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null.
* Other cases where the read value is of a different type than [T]
* due to an incorrect customization to [ObjectMapper].
*/
public inline fun <reified T> ObjectMapper.readValue(src: Reader): T = readValue(src, jacksonTypeRef<T>())
.checkTypeMismatch()
/**
* Shorthand for [ObjectMapper.readValue].
* @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null.
* Other cases where the read value is of a different type than [T]
* due to an incorrect customization to [ObjectMapper].
*/
public inline fun <reified T> ObjectMapper.readValue(src: InputStream): T = readValue(src, jacksonTypeRef<T>())
.checkTypeMismatch()
/**
* Shorthand for [ObjectMapper.readValue].
* @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null.
* Other cases where the read value is of a different type than [T]
* due to an incorrect customization to [ObjectMapper].
*/
public inline fun <reified T> ObjectMapper.readValue(src: ByteArray): T = readValue(src, jacksonTypeRef<T>())
.checkTypeMismatch()
/**
* Shorthand for [ObjectMapper.readValue].
* @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null.
* Other cases where the read value is of a different type than [T]
* due to an incorrect customization to [ObjectMapper].
*/
public inline fun <reified T> ObjectMapper.treeToValue(
n: TreeNode,
): T = readValue(this.treeAsTokens(n), jacksonTypeRef<T>()).checkTypeMismatch()
/**
* Shorthand for [ObjectMapper.convertValue].
* @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null.
* Other cases where the read value is of a different type than [T]
* due to an incorrect customization to [ObjectMapper].
*/
public inline fun <reified T> ObjectMapper.convertValue(from: Any?): T = convertValue(from, jacksonTypeRef<T>())
.checkTypeMismatch()
/**
* Shorthand for [ObjectReader.readValue].
* @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null.
* Other cases where the read value is of a different type than [T]
* due to an incorrect customization to [ObjectReader].
*/
public inline fun <reified T> ObjectReader.readValueTyped(jp: JsonParser): T = readValue(jp, jacksonTypeRef<T>())
.checkTypeMismatch()
/**
* Shorthand for [ObjectReader.readValues].
* @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null.
* Other cases where the read value is of a different type than [T]
* due to an incorrect customization to [ObjectReader].
*/
public inline fun <reified T> ObjectReader.readValuesTyped(jp: JsonParser): Iterator<T> {
val values = readValues(jp, jacksonTypeRef<T>())
return object : Iterator<T> by values {
override fun next(): T = values.next().checkTypeMismatch<T>()
}
}
public inline fun <reified T> ObjectReader.treeToValue(
n: TreeNode,
): T? = readValue(this.treeAsTokens(n), jacksonTypeRef<T>())
public inline fun <reified T, reified U> ObjectMapper.addMixIn(): ObjectMapper = addMixIn(T::class.java, U::class.java)
public inline fun <reified T, reified U> JsonMapper.Builder.addMixIn(): JsonMapper.Builder = addMixIn(
T::class.java,
U::class.java,
)
public inline fun <M : MapperBuilder<*, M>, reified T, reified U> M.addMixIn(): M = this.addMixIn(T::class.java, U::class.java)
public operator fun ArrayNode.plus(element: Boolean) {
add(element)
}
public operator fun ArrayNode.plus(element: Short) {
add(element)
}
public operator fun ArrayNode.plus(element: Int) {
add(element)
}
public operator fun ArrayNode.plus(element: Long) {
add(element)
}
public operator fun ArrayNode.plus(element: Float) {
add(element)
}
public operator fun ArrayNode.plus(element: Double) {
add(element)
}
public operator fun ArrayNode.plus(element: BigDecimal) {
add(element)
}
public operator fun ArrayNode.plus(element: BigInteger) {
add(element)
}
public operator fun ArrayNode.plus(element: String) {
add(element)
}
public operator fun ArrayNode.plus(element: ByteArray) {
add(element)
}
public operator fun ArrayNode.plus(element: JsonNode) {
add(element)
}
public operator fun ArrayNode.plus(elements: ArrayNode) {
addAll(elements)
}
public operator fun ArrayNode.plusAssign(element: Boolean) {
add(element)
}
public operator fun ArrayNode.plusAssign(element: Short) {
add(element)
}
public operator fun ArrayNode.plusAssign(element: Int) {
add(element)
}
public operator fun ArrayNode.plusAssign(element: Long) {
add(element)
}
public operator fun ArrayNode.plusAssign(element: Float) {
add(element)
}
public operator fun ArrayNode.plusAssign(element: Double) {
add(element)
}
public operator fun ArrayNode.plusAssign(element: BigDecimal) {
add(element)
}
public operator fun ArrayNode.plusAssign(element: BigInteger) {
add(element)
}
public operator fun ArrayNode.plusAssign(element: String) {
add(element)
}
public operator fun ArrayNode.plusAssign(element: ByteArray) {
add(element)
}
public operator fun ArrayNode.plusAssign(element: JsonNode) {
add(element)
}
public operator fun ArrayNode.plusAssign(elements: ArrayNode) {
addAll(elements)
}
public operator fun ArrayNode.minus(index: Int) {
remove(index)
}
public operator fun ArrayNode.minusAssign(index: Int) {
remove(index)
}
public operator fun ObjectNode.minus(field: String) {
remove(field)
}
public operator fun ObjectNode.minus(fields: Collection<String>) {
remove(fields)
}
public operator fun ObjectNode.minusAssign(field: String) {
remove(field)
}
public operator fun ObjectNode.minusAssign(fields: Collection<String>) {
remove(fields)
}
public operator fun JsonNode.contains(field: String): Boolean = has(field)
public operator fun JsonNode.contains(index: Int): Boolean = has(index)
public fun <T : Any> SimpleModule.addSerializer(
kClass: KClass<T>,
serializer: JsonSerializer<T>,
): SimpleModule = this.apply {
kClass.javaPrimitiveType?.let { addSerializer(it, serializer) }
addSerializer(kClass.javaObjectType, serializer)
}
public fun <T : Any> SimpleModule.addDeserializer(
kClass: KClass<T>,
deserializer: JsonDeserializer<T>,
): SimpleModule = this.apply {
kClass.javaPrimitiveType?.let { addDeserializer(it, deserializer) }
addDeserializer(kClass.javaObjectType, deserializer)
}
public inline fun <reified T : Any> ObjectMapper.configOverride(): MutableConfigOverride = configOverride(T::class.java)
public inline fun <reified T : Any> Module.SetupContext.configOverride(): MutableConfigOverride = configOverride(T::class.java)