Skip to content

Adding int support back for Enum, was available in 2.7.x #2160

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

Closed
wants to merge 1 commit into from

Conversation

sjoerdmulder
Copy link

While upgrading our code from 2.7 to 2.9 the deserializing of Enum's with @JsonValue and @JsonCreator is not working anymore.

Probally also fixes issue #1850

@plokhotnyuk
Copy link

Please, also, add tests to this case.

@cowtowncoder
Copy link
Member

Was about to suggest the same thing as @plokhotnyuk -- happy to merge but I do want to have a reproduction to ensure there's no regressions, as well as check that it is as expected (occasionally earlier functionality has worked accidentally, not by design).

@sjoerdmulder
Copy link
Author

@plokhotnyuk Yes, what would the right place to add a test? Sorry for this poor PR; Hacked it together during debugging our issue when upgrading to Jackson 2.9.

The following TestEnumCreator doesn't seem to work anymore while it worked in the past:

public enum TestEnumCreator {
    FOO(1),
    BAR(2);

    private final int value;

    TestEnumCreator(int value) {
        this.value = value;
    }
    
    @JsonCreator
    public static TestEnumCreator lookup(int value) {
        for (TestEnumCreator obj : values()) {
            if (obj.value == value) {
                return obj;
            }
        }
        return FOO;
    }
    
    @JsonValue
    public int getValue() {
        return value;
    }
}

We use this pattern to have somewhat rich enums that have more properties in the constructor than just the value, that's why there is the lookup method.

Somehow it seems that removing the @JsonCreator (and other constuctor args) does work;

public enum TestEnum {
    FOO(1),
    BAR(2);

    private final int value;

    TestEnum(int value) {
        this.value = value;
    }

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

}

@cowtowncoder
Copy link
Member

One quick question: does addition of mode help?

@JsonCreator(mode = JsonCreator.Mode.DELEGATING)

Problem being that in general single-argument Creator may be for mapping either using delegation (JSON maps directly into type exected) or "properties", in which JSON Object value would have 1 property with name matching Creator argument.
In absence of mode property, heuristics are used to try to figure out if:

  1. There is Creator argument name available (Java 8, parameter name inclusion enabled -> yes)
  2. There is matching potential property within value class for parameter to bind

So... it could be the case that logic here chose "properties"-based creator.

@sjoerdmulder
Copy link
Author

Yes mode helps, it then goes into https://github.com/FasterXML/jackson-databind/pull/2160/files#diff-ee7b0b6fecc26e6382bf6de025a352a4R107 with a IntegerDeserializer.

Awesome, i think this PR should be closed then but maybe it's worth documenting this in combination with Enum's somewhere?

@cowtowncoder
Copy link
Member

@sjoerdmulder Always happy to improve documentation (... and there are many places to do so, documenting lags behind code). Do you have a suggestion of specific improvement?

@sjoerdmulder
Copy link
Author

Not sure, but maybe a small section specifically about how Enum is handled?

@sjoerdmulder sjoerdmulder deleted the patch-1 branch October 24, 2018 07:55
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

Successfully merging this pull request may close these issues.

3 participants