Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.

Commit c945d51

Browse files
committed
Deserialization by creating your own custom deserialization annotations
1 parent a8d1f05 commit c945d51

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,93 @@ data class Dst(
268268
)
269269
```
270270

271+
#### Deserialization by creating your own custom deserialization annotations
272+
If you cannot use `KColumnDeserializer`, you can deserialize it by creating a custom deserialization annotations and adding it to the parameter.
273+
274+
275+
Custom deserialization annotation is made by defining a pair of `deserialization annotation` and `deserializer`.
276+
As an example, we will show how to create a `LocalDateTimeDeserializer` that deserializes from `String` to `LocalDateTime`.
277+
278+
##### Create deserialization annotation
279+
`@Target(AnnotationTarget.VALUE_PARAMETER)` and `KColumnDeserializeBy` annotation and several other annotations You can define a deserialization annotation by assigning a `KColumnDeserializeBy` annotation.
280+
281+
The argument of the `KColumnDeserializeBy` annotation requires the `KClass` of the deserializer.
282+
In this example, it is `LocalDateTimeDeserializerImpl`.
283+
284+
Also, arguments defined in the annotation can be referenced from the deserializer.
285+
286+
```kotlin
287+
@Retention(AnnotationRetention.RUNTIME)
288+
@MustBeDocumented
289+
@Target(AnnotationTarget.VALUE_PARAMETER)
290+
@KColumnDeserializeBy(LocalDateTimeDeserializerImpl::class)
291+
annotation class LocalDateTimeDeserializer(val pattern: String = "yyyy-MM-dd'T'HH:mm:ss")
292+
```
293+
294+
##### Create deserializer
295+
You can define `deserializer` by inheriting `AbstractKColumnDeserializer <A, S, D>`.
296+
Generics `A`,`S`,`D` have the following meanings.
297+
298+
- `A`: `deserialization annotation` `Type`.
299+
- `S`: Source `Type`.
300+
- `D`: Destination `Type`.
301+
302+
```kotlin
303+
class LocalDateTimeDeserializerImpl(
304+
annotation: LocalDateTimeDeserializer
305+
) : AbstractKColumnDeserializer<LocalDateTimeDeserializer, String, LocalDateTime>(annotation) {
306+
private val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern(annotation.pattern)
307+
308+
override val srcClass: Class<String> = String::class.javaObjectType
309+
310+
override fun deserialize(source: String?): LocalDateTime? {
311+
return source?.let {
312+
LocalDateTime.parse(it, formatter)
313+
}
314+
}
315+
}
316+
```
317+
318+
The `primary constructor` of the `deserializer` must request only the `deserialization annotation` specified in the `generics` as an argument.
319+
This is called when `KRowMapper` is initialized.
320+
321+
As shown in the example, you can refer to the annotation argument as you wish.
322+
323+
##### Using custom deserialization annotations
324+
The following is a summary of the `deserialization annotation` and `deserializer` created so far.
325+
326+
```kotlin
327+
@Retention(AnnotationRetention.RUNTIME)
328+
@MustBeDocumented
329+
@Target(AnnotationTarget.VALUE_PARAMETER)
330+
@KColumnDeserializeBy(LocalDateTimeDeserializerImpl::class)
331+
annotation class LocalDateTimeDeserializer(val pattern: String = "yyyy-MM-dd'T'HH:mm:ss")
332+
333+
class LocalDateTimeDeserializerImpl(
334+
annotation: LocalDateTimeDeserializer
335+
) : AbstractKColumnDeserializer<LocalDateTimeDeserializer, String, LocalDateTime>(annotation) {
336+
private val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern(annotation.pattern)
337+
338+
override val srcClass: Class<String> = String::class.javaObjectType
339+
340+
override fun deserialize(source: String?): LocalDateTime? {
341+
return source?.let {
342+
LocalDateTime.parse(it, formatter)
343+
}
344+
}
345+
}
346+
```
347+
348+
If you give it, you get the following.
349+
Since we can pass arbitrary arguments to `pattern`, we can see that it is highly flexible.
350+
351+
```kotlin
352+
data class Dst(
353+
@LocalDateTimeDeserializer(pattern = "yyyy-MM-dd'T'HH:mm:ss")
354+
val createTime: LocalDateTime
355+
)
356+
```
357+
271358
### Use default arguments
272359
`KRowMapper` supports `default arguments`.
273360
`Default arguments` are available in the following situations:

0 commit comments

Comments
 (0)