-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
JsonCreator factory method is ignored #660
Comments
This behavior is intentional, in that unless type is one of small-set of well-known types -- int, long, String, double, boolean -- annotation has to be used. So it is not a flaw or something to be changed, but rather established convention at this point (although could use more documentation). As to precedence between factory method, constructor, that is a judgment call. But at this point it really can not be changed without some explicit configuration setting -- maybe adding a I'll have to check what precedence is; and based on that I could add a config feature. |
I was looking for the serialization features that would define the priority for the factory method or constructor. I think this is a good way to solve this problem without affecting already implemented projects. |
@iovesnov Oh. Actually, looking at the example again, you are right in that explicit |
I am using Jackson version 2.4.4
Below two classes Test1 and Test2. Both classes has private constructor and JsonCreator method which accepts the single parameter. In case of Test1 class that parameter has 'double' type and Object in Test2 class.
I've added the code which converts JSON string into the Test1 and Test2 classes and as the result different methods are invoked. For the Test1 class invoked only constructor but for the Test2 factory method is invoked.
Code:
Output:
I debugged the Jackson a little bit and found that different types are treated differently: BasicDeserializerFactory#_handleSingleArgumentConstructor:
As you can see all Java types has a check isCreator || isVisible but not for the Object or custom class. This is really makes the difference because of the constructor with some Java types (like double or String) will override the factory method...
I personally think that Jackson should work the same independently to the object type.
It is you call but I would also say that factory method annotated with JsonCreator annotation should has higher priority then constructor.
The text was updated successfully, but these errors were encountered: