-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to allow functions *outside* of a class definition to be used as a JsonCreator or similar? #435
Comments
Interesting—this might be something best done in Databind where support for your proposed |
That could be, but I have a feeling it would be a weird ask for the Java-side of things. There are no free functions in Java, so the idea of a class having a creator method for a different (non-inheritance-related) class would be pretty weird. |
@ragnese the usual split would be that Mechanism that exists (at very general level) is One possibly related feature that has been requested before, fwtw, is the ability to use actual static factory method defined in a mix-in class, instead of only using annotations from mix-in class. |
Interesting. I'm not familiar with what you're describing about using annotations from mix-in classes (and therefore I also don't understand the request feature you're describing). Could you elaborate? I'm not even sure what "mix-in" means in this specific context. |
@ragnese Jackson allows addition of "mix-in annotations", explained f.ex here: so you can basically say "use annotations this class/interface has same as if they were part of class [target class]". |
Use case
Basically, the idea is that I might design a class where I want to use a factory function without needing to write:
Describe the solution you'd like
I rather just be able to say:
or maybe:
Describe alternatives you've considered
My first option was to do:
Which is almost perfectly fine. Now I can just call
MyClass(arg1, arg2)
in my regular code because of theoperator invoke
and it works as a JsonCreator just fine. However, if I have to reference the invoke function in other code, I have to writeMyClass.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".The text was updated successfully, but these errors were encountered: