jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

Serialize enums to lowercase

Open vojkny opened this issue 3 years ago • 6 comments

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 avatar Feb 12 '21 13:02 vojkny

See proposed PR.

vojkny avatar Feb 12 '21 13:02 vojkny

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 avatar Feb 12 '21 18:02 cowtowncoder

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.

kdankert avatar May 10 '21 10:05 kdankert

@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.).

cowtowncoder avatar May 10 '21 17:05 cowtowncoder

No problem, thanks for the update.

kdankert avatar May 10 '21 18:05 kdankert

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)
}

vojkny avatar May 11 '21 07:05 vojkny

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?

ZeroOne3010 avatar Nov 11 '22 13:11 ZeroOne3010

@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)

cowtowncoder avatar Nov 14 '22 02:11 cowtowncoder

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

ZeroOne3010 avatar Nov 14 '22 12:11 ZeroOne3010

Implemented via #3776.

cowtowncoder avatar Feb 10 '23 04:02 cowtowncoder