@@ -108,6 +108,86 @@ public class ByStaticMethod {
108
108
}
109
109
```
110
110
111
+ #### Define custom deserializer annotation
112
+ ` KRowMapper ` supports complex deserialization by defining custom deserializer ` annotations ` .
113
+ As an example, a custom deserializer ` annotation ` that performs deserialization from a ` String ` to ` LocalDateTime ` is shown.
114
+
115
+ ``` kotlin
116
+ // annotation
117
+ @Target(AnnotationTarget .VALUE_PARAMETER )
118
+ @Retention(AnnotationRetention .RUNTIME )
119
+ @MustBeDocumented
120
+ @KColumnDeserializeBy(LocalDateTimeDeserializerImpl ::class )
121
+ annotation class LocalDateTimeDeserializer (val pattern : String = " yyyy-MM-dd'T'HH:mm:ss" )
122
+
123
+ // deserializer
124
+ class LocalDateTimeDeserializerImpl (
125
+ annotation : LocalDateTimeDeserializer
126
+ ) : AbstractKColumnDeserializer<LocalDateTimeDeserializer, String, LocalDateTime>(annotation) {
127
+ private val formatter: DateTimeFormatter = DateTimeFormatter .ofPattern(annotation.pattern)
128
+
129
+ override val srcClass: Class <String > = String ::class .javaObjectType
130
+
131
+ override fun deserialize (source : String? ): LocalDateTime ? {
132
+ return source?.let {
133
+ LocalDateTime .parse(it, formatter)
134
+ }
135
+ }
136
+ }
137
+ ```
138
+
139
+ ``` kotlin
140
+ // usage
141
+ data class Dst (@LocalDateTimeDeserializer val dateTime : LocalDateTime )
142
+ ```
143
+
144
+ ##### annotation
145
+ For the ` annotation class ` , specify the deserializer ` class ` with the ` KColumnDeserializeBy ` ` annotation ` .
146
+ Also, the fields prepared for this ` annotation class ` can be used from the deserializer.
147
+
148
+ ``` kotlin
149
+ @Target(AnnotationTarget .VALUE_PARAMETER )
150
+ @Retention(AnnotationRetention .RUNTIME )
151
+ @MustBeDocumented
152
+ // LocalDateTimeDeserializerImpl is deserializer class
153
+ @KColumnDeserializeBy(LocalDateTimeDeserializerImpl ::class )
154
+ annotation class LocalDateTimeDeserializer (val pattern : String = " yyyy-MM-dd'T'HH:mm:ss" )
155
+ ```
156
+
157
+ ##### deserializer
158
+ Deserializer is created by inheriting ` AbstractKColumnDeserializer ` .
159
+ The meaning of each type parameter is as follows.
160
+
161
+ - ` A ` : ` Annotation class ` (` LocalDateTimeDeserializer ` in this example)
162
+ - ` S ` : ` Java class ` of argument required at deserialization
163
+ - ` D ` : ` Class ` returned after deserialization
164
+
165
+ ``` kotlin
166
+ abstract class AbstractKColumnDeserializer <A : Annotation , S : Any , D : Any >(protected val annotation : A ) {
167
+ abstract val srcClass: Class <S >
168
+ abstract fun deserialize (source : S ? ): D ?
169
+ }
170
+ ```
171
+
172
+ In the example, deserialization from a ` String ` to ` LocalDateTime ` is performed based on the ` pattern ` obtained from the
173
+ ` LocalDateTimeDeserializer ` .
174
+
175
+ ``` kotlin
176
+ class LocalDateTimeDeserializerImpl (
177
+ annotation : LocalDateTimeDeserializer
178
+ ) : AbstractKColumnDeserializer<LocalDateTimeDeserializer, String, LocalDateTime>(annotation) {
179
+ private val formatter: DateTimeFormatter = DateTimeFormatter .ofPattern(annotation.pattern)
180
+
181
+ override val srcClass: Class <String > = String ::class .javaObjectType
182
+
183
+ override fun deserialize (source : String? ): LocalDateTime ? {
184
+ return source?.let {
185
+ LocalDateTime .parse(it, formatter)
186
+ }
187
+ }
188
+ }
189
+ ```
190
+
111
191
## Installation
112
192
Published on JitPack.
113
193
You can use this library on ` maven ` , ` gradle ` and any other build tools.
0 commit comments