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

@JsonCreator not working on a factory with multiple arguments for a enum type #929

Closed
logic-1 opened this issue Sep 12, 2015 · 2 comments

Comments

@logic-1
Copy link

commented Sep 12, 2015

Using a factory method having multiple arguments annotated with @JsonCreator works normally when the factory is for a class type, but fails if the factory is for an enum type.

jackson-databind v 2.5.1
jackson-annotations v 2.5.0

e.g. code

String json = "{\"intProp\":1,\"stringProp\":\"first\"}";
ObjectMapper mapper = new ObjectMapper();

MyClass classValue = mapper.readValue(json, MyClass.class); //works
MyEnum enumValue = mapper.readValue(json, MyEnum.class); //fails 

both MyClass and MyEnum has a method like

@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public static MyClass/MyEnum forValues(@JsonProperty("intProp") int intProp, @JsonProperty("stringProp") String stringProp){

        /// logic to create an instance with params using int
        // in case of enum find the appropriate object. 
        return object; 
}
@cowtowncoder

This comment has been minimized.

Copy link
Member

commented Sep 14, 2015

I can confirm this. Support for multi-argument creators does not yet exist; exception is thrown with "Unsuitable method decorated with @JsonCreator (for Enum type )".
Would be good to add support.

@ralfstuckert

This comment has been minimized.

Copy link

commented Dec 10, 2015

+1 for this issue. I prefer serialization either as a simple value (using @JsonValue), or as "an Object" instead of standard enum values. By doing so you are giving the consumer side the chance to use regular (non-enum) classes. Having to use enums on "both sides" introduces undesirable tight coupling.

@cowtowncoder cowtowncoder added the 2.9 label May 5, 2016

@cowtowncoder cowtowncoder removed the 2.9 label Sep 16, 2016

@cowtowncoder cowtowncoder added this to the 2.8.3 milestone Sep 16, 2016

cowtowncoder added a commit that referenced this issue Sep 16, 2016

tolkonepiu added a commit to tolkonepiu/jackson-databind that referenced this issue Sep 27, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.