Skip to content

Commit

Permalink
Fix #929 (Multi-args @JsonCreator for Enums)
Browse files Browse the repository at this point in the history
  • Loading branch information
kulabun committed Sep 15, 2016
1 parent bb06aa0 commit 129f9ba
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1241,20 +1241,16 @@ public JsonDeserializer<?> createEnumDeserializer(DeserializationContext ctxt,
// May have @JsonCreator for static factory method:
for (AnnotatedMethod factory : beanDesc.getFactoryMethods()) {
if (ctxt.getAnnotationIntrospector().hasCreatorAnnotation(factory)) {
int argCount = factory.getParameterCount();
if (argCount == 1) {
Class<?> returnType = factory.getRawReturnType();
// usually should be class, but may be just plain Enum<?> (for Enum.valueOf()?)
if (returnType.isAssignableFrom(enumClass)) {
deser = EnumDeserializer.deserializerForCreator(config, enumClass, factory, valueInstantiator, creatorProps);
break;
}
} else if (argCount == 0) { // [databind#960]
if (factory.getParameterCount() == 0) { // [databind#960]
deser = EnumDeserializer.deserializerForNoArgsCreator(config, enumClass, factory);
break;
}
throw new IllegalArgumentException("Unsuitable method ("+factory+") decorated with @JsonCreator (for Enum type "
+enumClass.getName()+")");
Class<?> returnType = factory.getRawReturnType();
// usually should be class, but may be just plain Enum<?> (for Enum.valueOf()?)
if (returnType.isAssignableFrom(enumClass)) {
deser = EnumDeserializer.deserializerForCreator(config, enumClass, factory, valueInstantiator, creatorProps);
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property)
throws JsonMappingException
{
if ((_deser == null) && (_inputType != null)) {
if ((_deser == null) && (_inputType != null) && (_creatorProps == null)) {
return new FactoryBasedEnumDeserializer(this,
ctxt.findContextualValueDeserializer(_inputType, property));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ public String toString() {
}
}


static enum Enum929
{
A, B, C;

@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
static Enum929 forValues(@JsonProperty("id") int intProp,
@JsonProperty("name") String name)
{
return Enum929.valueOf(name);
}
}

/*
/**********************************************************
/* Test methods
Expand Down Expand Up @@ -272,4 +285,11 @@ public void testEnumCreators1291() throws Exception
Enum1291 result = mapper.readValue(json, Enum1291.class);
assertSame(Enum1291.V2, result);
}

// for [databind#929]
public void testMultiArgEnumCreator() throws Exception
{
Enum929 v = MAPPER.readValue("{\"id\":3,\"name\":\"B\"}", Enum929.class);
assertEquals(Enum929.B, v);
}
}

This file was deleted.

0 comments on commit 129f9ba

Please sign in to comment.