FasterXML / FasterXML/jackson-module-kotlin
Is it possible to allow functions *outside* of a class definition to be used as a JsonCreator or similar?
- Dominant language
- Kotlin
- Stars
- 1.2k
- Forks
- 187
- Avg merge
- 9h 48m
- Merged PRs (30d)
- 16
Description
**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".
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.