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

Commit a8d1f05

Browse files
committed
Deserialization by using the KColumnDeserializer annotation
1 parent dda8e86 commit a8d1f05

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,46 @@ To deal with this problem, `KRowMapper` provides the following three types of de
228228
2. Deserialization by creating your own custom deserialization annotations.
229229
3. Deserialization from multiple arguments.
230230

231+
#### Deserialization by using the KColumnDeserializer annotation
232+
If it is a self-made class and can be initialized from a single argument, deserialization using the `KColumnDeserializer` annotation can be used.
233+
`KColumnDeserializer` annotation can be used to `constructor` or `factory method` defined in `companion object`.
234+
235+
```kotlin
236+
// for primary constructor
237+
data class FooId @KColumnDeserializer constructor(val id: Int)
238+
```
239+
240+
```kotlin
241+
// for secondary constructor
242+
data class FooId(val id: Int) {
243+
@KColumnDeserializer
244+
constructor(id: String) : this(id.toInt())
245+
}
246+
```
247+
248+
```kotlin
249+
// for factory method
250+
data class FooId(val id: Int) {
251+
companion object {
252+
@KColumnDeserializer
253+
fun of(id: String): FooId = FooId(id.toInt())
254+
}
255+
}
256+
```
257+
258+
Class with `KColumnDeserializer` annotation can be mapped as an argument without any special description.
259+
260+
```kotlin
261+
data class Dst(
262+
fooId: FooId,
263+
bar: String,
264+
baz: Int?,
265+
266+
...
267+
268+
)
269+
```
270+
231271
### Use default arguments
232272
`KRowMapper` supports `default arguments`.
233273
`Default arguments` are available in the following situations:

0 commit comments

Comments
 (0)