-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Specifying Enum
value serialization using @JsonProperty
#677
Comments
Interesting idea. No, currently there isn't, but I think we should be able to repurpose something. |
Agreed that repurposing
|
Ugh. While a reasonable idea, I don't know how to implement this -- Java enums are a nasty kludge under the hood, so I don't quite know how to locate annotations that hide in there. It is doable, but not a quick fix as I hoped it would be. FWIW, to solve it for specific app, trick is to override |
Would something like this work? @Override
public String findEnumValue(Enum<?> value) {
JsonProperty jsonPropertyAnnotation = value.getClass().getField(value.name()).getAnnotation(JsonProperty.class);
if (jsonPropertyAnnotation != null && !jsonPropertyAnnotation.value().equals(JsonProperty.USE_DEFAULT_NAME)) {
return jsonPropertyAnnotation.value();
} else {
return value.name();
}
} Source for reflection code: http://stackoverflow.com/a/7260009 |
Sounds like that could work. One issue is that accessing annotation directly does not allow processing of mix-in annotations; but trying to make that work would be significant hassle. |
Enum
value serialization using @JsonProperty
I think it just might be possible to use |
On second thought: looking at how So given all of this, I don't see an issue with going with @allenchen1154's suggestion. |
This functionality should be documented somewhere in |
@Raniz85 appreciate your suggesting this (makes sense): could you please file a new issue for |
Done here |
Thanks! |
Currently, if I want to deserialize an enum with a value that isn't its
Enum.name()
, I can do eitheror, using
DeserializationFeature.READ_ENUMS_USING_TO_STRING
,This seems like a lot of boilerplate - is there a simpler way to do this, similar to how
Gson
handles it?It's both more concise and handles both serialization and deserialization.
The text was updated successfully, but these errors were encountered: