FasterXML / FasterXML/jackson-module-kotlin
Is it possible to allow functions *outside* of a class definition to be used as a JsonCreator or similar?
- Lingua principale
- Kotlin
- Stelle
- 1.2k
- Fork
- 187
- Merge medio
- 9h 48m
- PR unite (30g)
- 16
Descrizione
**Use case**
Basically, the idea is that I might design a class where I want to use a factory function without needing to write:
```
companion object {
@JvmStatic
@JsonCreator
private fun creator(arg1: Arg1, arg2: Arg2) = myFactoryFunction(arg1, arg2)
}
```
**Describe the solution you'd like**
I rather just be able to say:
```
@JsonDeserialize(with = ::myFactoryFunction)
class MyClass () {}
```
or maybe:
```
@JsonCreator(for = MyClass::class)
fun myFactoryFunction(arg1: Arg1, arg2: Arg2): MyClass = TODO()
```
**Describe alternatives you've considered**
My first option was to do:
```
companion object {
@JvmStatic
@JsonCreator
operator fun invoke(arg1: Arg1, arg2: Arg2) = TODO()
}
```
Which is *almost* perfectly fine. Now I can just call `MyClass(arg1, arg2)` in my regular code because of the `operator invoke` and it works as a JsonCreator just fine. However, if I have to *reference* the invoke function in other code, I have to write `MyClass.Companion::invoke` instead of `::MyClass` like you would for a true constructor or a top-level factory function. So, since I hate the ugly invoke reference, I've taken to writing the top-level factory function and then writing a private companion function as the JsonCreator that does nothing but call the true factory function. I'd like to eliminate this "boilerplate".
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Valutazione
Questa issue non è ancora stata valutata.