Open cns-solutions-admin opened 2 years ago
Sounds like a good idea, at high level -- I'll have think about details some more. But I agree that the ability to use external factory object/methods would be useful. It could be an extension of @JsonDeserialize
as suggested, or possible a new annotation like @JsonFactory
.
I'd be happy to help if you or anyone wanted to try to implement this; I may not have much time in near future to work on it myself (but who knows), but always willing to help others get PRs through.
On Thu, Jul 7, 2022 at 9:18 AM JulienElkaim @.***> wrote:
Hello,
I am not a huge fan of the current choice that we have to modify/annotate our classes to make the objects deserializable. I would love to be able to use something like this:
public class Item { // Fully POJO, no Jackson element on it private String name; private Integer ref; ... }
@JsonCreatorProvider public ItemFactory { @JsonCreator public static Item @.***("name") String name, @JsonProperty("ref") Integer ref) { ... } }
@cowtowncoder https://github.com/cowtowncoder , would you accept this concept if I submit such PR? Thinking about it now, I assume it would require reflexion to discover JsonCreator without the hint from a @JsonDeserialize annotation on the deserialize object's class.
Why not add this option to the ObjectMapper to scan a package for @JsonCreatorProvider, and while trying to deserialize an object if no creator is defined on the class, then look in cached JsonCreators discovered during the reflection phase at ObjectMapper initialization?
The only point I am afraid of is that the concept of making the ObjectMapper responsible to store and know how to deserialize does not fit with the current philosophy to make objects responsible to declare how they will be deserialized.
Good question; I am glad you asked!
I would not accept something that requires classpath scanning to auto-detect providers; Jackson does not use classpath scanning now and I don't want to add it (too fragile, too much black magic for core system; probably would also require dependency to a library that handles it).
But I would be fine with added extension points for registering such providers/factories, if necessary; someone can then create a module that uses whatever mechanism for auto-detection. So I am not against functionality itself, only inclusion within core.
Message ID: @.***>
Feature Support static factory methods in factory classes. The factory method should be able to accept properties like a class-local creator method.
Problem solved Currently a creator method must be within the same class. Factory methods in other classes are not supported. Thus, if the constructor of a non-modifiable class (e.g. from a library) is non-accessible and the factory method is in another class, the only workaround is to use CAN_OVERRIDE_ACCESS_MODIFIERS, which is not possible in some environments.
Examples classes that cannot be deserialized currently in environments, where access modifiers cannot be overridden:
public FactoryForClassWithoutAccessibleConstructor { public static createClassWithoutAccessibleConstructor() { ... } }
Proposed implementation
Usage example:
public class MyFactory { @JsonCreator public static MyBean createMyBean(@JsonProperty(...) ...) { ... } }
Additional context