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
Closed
Milestone

Comments

@logic-1
Copy link

logic-1 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
Copy link
Member

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
Copy link

+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.

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

3 participants