Skip to content
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

Improve Enum handling parity with Jackson #818

Merged
merged 4 commits into from
Apr 22, 2024

Conversation

oujesky
Copy link
Contributor

@oujesky oujesky commented Apr 11, 2024

Improve enum serialization and deserialization to move closer to parity with Jackson

  • Additional Jackson annotations support:

    • deserialize enums with @JsonValue
    • serialize and deserialize enums with @JsonProperty
    • This follows the same precedence in evaluation as Jackson:
  • Improved EnumSet de/serialization of contained enums

  • Added support for EnumMap

@CLAassistant
Copy link

CLAassistant commented Apr 11, 2024

CLA assistant check
All committers have signed the CLA.

@oujesky
Copy link
Contributor Author

oujesky commented Apr 11, 2024

This should fix #503 and #780

@yawkat yawkat requested a review from dstepanov April 12, 2024 08:00
@yawkat yawkat added the type: improvement A minor improvement to an existing feature label Apr 12, 2024

@NonNull
private E transform(@NonNull Decoder decoder, Object value) throws IOException {
for (EnumBeanIntrospection.EnumConstant<? super E> enumConstant : enumIntrospection.getConstants()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you precalculate some of this stuff? Maybe create a map instead of interesting on every deserialization

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added pre-calculation in 9d4e089


@Override
public void serialize(@NonNull Encoder encoder, @NonNull EncoderContext context, @NonNull Argument<? extends E> type, E value) throws IOException {
for (EnumBeanIntrospection.EnumConstant<? extends E> enumConstant : enumIntrospection.getConstants()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to precalculate constants as a map

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added pre-calculation in 9d4e089

public E deserialize(@NonNull Decoder decoder, @NonNull DecoderContext context, @NonNull Argument<? super E> type) throws IOException {
var value = decoder.decodeString();

for (EnumBeanIntrospection.EnumConstant<? super E> enumConstant : enumIntrospection.getConstants()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, please try to precalculate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added pre-calculation in 9d4e089

@dstepanov
Copy link
Contributor

Please fix checkstyle

@dstepanov
Copy link
Contributor

I have refactored caching a bit, please check.

@altro3
Copy link
Contributor

altro3 commented Apr 19, 2024

I think, need add test, when you use @JsonValue on method, like this:

@Serdeable
enum MyEnum {

    VALUE1("value_1"),
    VALUE2("value_2"),
    VALUE3("value_3");

    private final String value;

    MyEnum(String value) {
        this.value = value;
    }

    @JsonValue
    public String getValue() {
        return value;
    }
}

@altro3
Copy link
Contributor

altro3 commented Apr 19, 2024

oh, It already exists. Then everything looks good to me

@dstepanov dstepanov merged commit bd82565 into micronaut-projects:2.10.x Apr 22, 2024
12 checks passed
@dstepanov
Copy link
Contributor

Thanks @oujesky !

andriy-dmytruk pushed a commit that referenced this pull request Jan 15, 2025
Improve Enum handling parity with Jackson (#818)
andriy-dmytruk pushed a commit that referenced this pull request Jan 15, 2025
Improve Enum handling parity with Jackson (#818)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: improvement A minor improvement to an existing feature
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants