Skip to content
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

Add new factory method for creating custom EnumValues to pass to EnumDeserializer #2663

Closed
whut opened this issue Mar 24, 2020 · 3 comments
Closed
Milestone

Comments

@whut
Copy link

whut commented Mar 24, 2020

The com.fasterxml.jackson.databind.util.EnumValues constructor is now private, which means that it is
not possible to create EnumSerializer instance with custom EnumValues. It should be at least protected, like is the case of com.fasterxml.jackson.databind.util.EnumResolver used by EnumDeserializer.

I try to implement different strategy for serialization and deserialization of enums, simply to convert java SNAKE_CASE to more common in JSON camelCase and vice-versa. It is quite easy with EnumDeserializer, but in case of EnumSerializer it is not. I would like to just create new instance of EnumSerializer with EnumValues instance build using different logic. Currently the only way (without reflection which I will use for the time being) is to cal the EnumValues#construct* methods, which have hard-coded enum-to-string mapping logic.

BTW. Consider adding casing strategy for enum serialization/deserialization as build-in jackson feature. For example in JSON.NET it is just a matter of passing approprate NamingStrategy to StringEnumConverter constructor.

@cowtowncoder
Copy link
Member

If a constructor is private, there tends to be specific reason for that, and a factory method to call it. So I do not think changing that access method is usually the right way to go -- it just opens up a fragile, unsupported extension point; something that will likely break when databind needs to pass some more information. Not sure why constructor for deserializer is left non-private; perhaps an oversight.
Granted, use of reflection is even worse, and even more likely to break with new versions (private methods not being considered part of API and changeable on any minor version).

But adding an alternative factory method could be a slightly better choice. Perhaps one that would take PropertyNamingStrategy (along your suggestion).
While it is designed for property name handling, it could probably be used for Enums as values as defined as... Methods I think? (or possibly Fields, would need to check).

I am open to other ideas, PRs.

@cowtowncoder
Copy link
Member

cowtowncoder commented Mar 25, 2020

One quick thing that I can easily add, and have no problems with is to just add matching factory method in 2.11. I'd prefer keeping constructor private in this case for now, since class is already final ((package) protected would be needed for sub-classing).

@cowtowncoder cowtowncoder changed the title EnumValues should have accessible constructor to be able to create custom EnumSerializer instance Add new factory method for creating custom EnumValues to pass to EnumDeserializer Mar 25, 2020
@cowtowncoder cowtowncoder added this to the 2.11.0 milestone Mar 25, 2020
cowtowncoder added a commit that referenced this issue Mar 26, 2020
@cowtowncoder
Copy link
Member

Added 2 construct methods that will allow constructing specific instance necessary.

I think idea of supporting naming strategies in some form is good, and so although there are some open questions, will create a follow-up issue for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants