Skip to content

[Feature request] Use @JsonValue annotation #2

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

Open
caarmen opened this issue Dec 4, 2021 · 4 comments
Open

[Feature request] Use @JsonValue annotation #2

caarmen opened this issue Dec 4, 2021 · 4 comments

Comments

@caarmen
Copy link

caarmen commented Dec 4, 2021

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

@caarmen caarmen changed the title Use @JsonValue annotation [Feature request] Use @JsonValue annotation Dec 4, 2021
@pjfanning
Copy link
Owner

@caarmen I had a quick look and the Java annotation seems to be missing on the class. Seems like this might be related to that dotty issue you linked to FasterXML/jackson-module-scala#532

@pjfanning
Copy link
Owner

@caarmen I added scala/scala3#12492 (comment) - but while that issue is similar to this, I think it is different. That issue relates to the @getter being ignored (Scala 3 does not not ignore the @JsonValue when the @getter is removed but that changes the meaning of the @JsonValue annotation so it doesn't work).

In this case, there is an annotation on the enum class and Scala 3 seems to ignore it regardless of the @getter annotation.

@pjfanning
Copy link
Owner

actually, I think the existing dotty issue might fix this issue - but you will need

@(JsonValue @getter)

instead of just @JsonValue

The core jackson-databind does not seem to support @JsonValue on fields - that you seem to need to have the annotation on a method and the @getter meta annotation means the annotation gets added to the getter method instead of the field (both called after the field name).

@pjfanning
Copy link
Owner

If you use @JsonValue - you need to add a @JsonCreator annotation to support the deserialization case - see https://stackoverflow.com/questions/57896888/jackson-jsonvalue-annotated-field-cannot-be-deserialized

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants