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

Allow serializing enums to lowercase (EnumFeature.WRITE_ENUMS_TO_LOWERCASE) #3053

Closed
vojkny opened this issue Feb 12, 2021 · 10 comments
Closed
Labels
2.15 Issues planned for 2.15 enum Related to handling of Enum values
Milestone

Comments

@vojkny
Copy link

vojkny commented Feb 12, 2021

Is your feature request related to a problem? Please describe.
Java enums are usually defined as UPPER_CASE_WITH_UNDERSCORE. This is not a standard in endpoints, as these rather use lower_case_with_underscore. There is already feature for ACCEPT_CASE_INSENSITIVE_ENUMS, which helps with deserialization, the feature for serialization is however currently missing. Only option is using @JsonProperty or toString() serialization.

Describe the solution you'd like
I would like to have SerializationFeature.WRITE_ENUM_LOWERCASED.

Usage example
Any API endpoint.

@vojkny vojkny added the to-evaluate Issue that has been received but not yet evaluated label Feb 12, 2021
@vojkny
Copy link
Author

vojkny commented Feb 12, 2021

See proposed PR.

@cowtowncoder
Copy link
Member

I don't think I'll accept this as a SerializationFeature as it is little bit too specific (I know, there are some ENUM-specific ones already but I'd rather want to get rid of them).
But there probably should be separate EnumConfig / EnumFeature set for things to configure Enum handling with.

Or, alternatively, have something like PropertyNamingStrategy but for Enum values.

I'll keep this issue open but the solution will need to be something other than another SerializationFeature (which very likely also would require matching DeserializationFeature).

@cowtowncoder cowtowncoder added 2.13 and removed to-evaluate Issue that has been received but not yet evaluated labels Feb 12, 2021
@kdankert
Copy link

I see you removed the to evaluate label on this issue. Is this going to be implemented soon, when is Jackson 2.13 going to be released, as the information in the wiki don't seem to be right anymore.

@cowtowncoder
Copy link
Member

cowtowncoder commented May 10, 2021

@kdankert I probably will not have time to work on this any time soon unfortunately.

2.13 will be released whenever it might be ready: versions are not scheduled based on time. But before release there will be one or more Release Candidates and announcements will typically be sent on dev mailing list (as well as via FasterXML Twitter account.).

@kdankert
Copy link

No problem, thanks for the update.

@vojkny
Copy link
Author

vojkny commented May 11, 2021

Note that I solved it temporarily with custom serializer:

package net.goout.jackson

import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.ser.std.StdSerializer

val lowerCaseEnumJacksonSerializerModule = SimpleModule().also {
    val lowerCaseEnumKeySerializer = object : StdSerializer<Enum<*>>(Enum::class.java) {
        override fun serialize(value: Enum<*>?, json: JsonGenerator, provider: SerializerProvider) {
            json.writeFieldName(value?.name?.toLowerCase())
        }
    }
    val lowerCaseEnumValueSerializer = object : StdSerializer<Enum<*>>(Enum::class.java) {
        override fun serialize(value: Enum<*>?, json: JsonGenerator, provider: SerializerProvider) {
            json.writeString(value?.name?.toLowerCase())
        }
    }
    it.addKeySerializer(Enum::class.java, lowerCaseEnumKeySerializer)
    it.addSerializer(Enum::class.java, lowerCaseEnumValueSerializer)
}

@cowtowncoder cowtowncoder added 2.14 and removed 2.13 labels Jul 15, 2021
@cowtowncoder cowtowncoder added the enum Related to handling of Enum values label Aug 3, 2022
@ZeroOne3010
Copy link

So Jackson 2.14 is out and I'm unable to use this feature, the new SerializationFeature.WRITE_ENUMS_LOWERCASED enum value is not there yet. Should the label be updated or am I doing something wrong?

@cowtowncoder cowtowncoder added 2.15 Issues planned for 2.15 and removed 2.14 labels Nov 14, 2022
@cowtowncoder
Copy link
Member

@ZeroOne3010 Unfortunately this feature did not make it in due to timing constraints, all the other work. I changed the label -- it is intended to show release targeted, but is not binding (i.e. aspirational, not constraining)

@ZeroOne3010
Copy link

@cowtowncoder OK, no worries, thanks for the clarification! I can wait. :)

@cowtowncoder cowtowncoder changed the title Serialize enums to lowercase Allow serializing enums to lowercase (EnumFeature.WRITE_ENUMS_TO_LOWERCASE) Feb 10, 2023
cowtowncoder added a commit that referenced this issue Feb 10, 2023
cowtowncoder added a commit that referenced this issue Feb 10, 2023
@cowtowncoder cowtowncoder added this to the 2.15.0 milestone Feb 10, 2023
@cowtowncoder
Copy link
Member

Implemented via #3776.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.15 Issues planned for 2.15 enum Related to handling of Enum values
Projects
None yet
Development

No branches or pull requests

4 participants