jackson-databind
jackson-databind copied to clipboard
Serialize enums to lowercase
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.
See proposed PR.
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
).
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 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.).
No problem, thanks for the update.
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)
}
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 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 OK, no worries, thanks for the clarification! I can wait. :)
Implemented via #3776.