Skip to content

[Feature request] Use @JsonValue annotation #2

Open
@caarmen

Description

@caarmen

Currently, the EnumSerializerModule serializes enums by using their toString :

def serialize(value: Enum, jgen: JsonGenerator, provider: SerializerProvider): Unit =
  provider.defaultSerializeValue(value.toString, jgen)

It would be nice to look for a field with a @JsonValue annotation, and use that instead of toString(), if it exists.

Example use case:

enum PartOfSpeech(@JsonValue val modelName: String):

  case NOUN extends PartOfSpeech("noun")
  case ADJECTIVE extends PartOfSpeech("adjective")
  case ADVERB extends PartOfSpeech("adverb")
  case VERB extends PartOfSpeech("verb")
  case UNKNOWN extends PartOfSpeech("unknown")

end PartOfSpeech

I'd like to see noun instead of NOUN in the json.

For now, my workaround is to override toString:

override def toString: String = modelName

This workaround seems to work just fine. I thought it might be a nice bonus to be able to use the @JsonValue annotation though.

⚠️ This requested feature may not work in scala3 if the annotation is on a constructor parameter, because of this issue: FasterXML/jackson-module-scala#532

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions