I am trying to de-serialize json to enumerate using jackson. If the factory method has only one argument, it works fine. As soon as we add more arguments, it stops working.
here is an example of the code i tried.
public enum Test {
FIRST(1, "first");
private final int intProp;
private final String stringProp;
Test(int i, String stringProp) {
this.stringProp = stringProp;
this.intProp = i;
}
private static final Map<String, Test> allEntries = new HashMap<>(Test.values().length);
static {
for(Test each : Test.values()){
allEntries.put(getCode(each.intProp, each.stringProp), each);
}
}
private static String getCode(int i, String s){
return s + i;
}
@JsonCreator(mode = Mode.PROPERTIES)
public static Test forValues(@JsonProperty("intProp") int intProp,
@JsonProperty("stringProp") String stringProp
){
return allEntries.get(getCode(intProp,stringProp));
}
}
using the following code to json deserialize
String json = "{\"intProp\":1,\"stringProp\":\"first\"}";
ObjectMapper mapper = new ObjectMapper();
Test enumValue = mapper.readValue(json, Test.class);
This is the exception I get
com.fasterxml.jackson.databind.JsonMappingException: Unsuitable method
Versions: jackson-databind 2.5.1, jackson-annotations 2.5.0
I do not want to write a custom deserializer, is there an error in my approach or is the option simply not supported by jackson?
The same thing works when used classinstead enum.