question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serialize enums to lowercase

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:11
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
knyttlcommented, 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)
}
0reactions
ZeroOne3010commented, Nov 14, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson databind enum case insensitive - java - Stack Overflow
Can anyone suggest a better way to serialize and deserialize enums with proper naming convention? I don't want my enums in java to...
Read more >
How To Serialize and Deserialize Enums with Jackson
In this quick tutorial, we'll learn how to control the way Java Enums are serialized and deserialized with Jackson 2.
Read more >
Serializing Enum Values in Lowercase - MSDN - Microsoft
I have a few enums I'm using, and, when they get serialized, I'd like to have them be in lowercase. I found this...
Read more >
Gson Custom Enum TypeAdapterFactory - Bram Yeh
In previous blog, I suggest to use LowercaseEnumTypeAdapterFactory on Gson.Builder to convert enums to lowercase string, despite the fact that they're ...
Read more >
How to Serialize Enum to a String in C# - Code Maze
In C#, JSON serialization very often needs to deal with enum objects. By default, enums are serialized in their integer form.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found