You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using Jackson 2.12.3 and it seems that Jackson will sometimes take preference for a private constructor over a public static factory method that is annotated with @JsonCreator.
This example showcases the problem:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new ParameterNamesModule());
ValueObject valueObject = objectMapper.readValue("{\"value\":\"ABC-123\"}", ValueObject.class);
System.out.println("valueObject = " + valueObject);
System.out.println("----");
WrapperObject wrapperObject = objectMapper.readValue("{\"valueObject\":\"ABC-123\"}", WrapperObject.class);
System.out.println("wrapperObject = " + wrapperObject);
}
public static class ValueObject {
private final String value;
private ValueObject(String value) {
System.out.println("constructor: " + value);
this.value = value;
}
@JsonCreator
public static ValueObject of(String value) {
System.out.println("factory: " + value);
return new ValueObject(value);
}
public String getValue() {
return value;
}
}
public static class WrapperObject {
private final ValueObject valueObject;
@JsonCreator
public WrapperObject(ValueObject valueObject) {
this.valueObject = valueObject;
}
public ValueObject getValueObject() {
return valueObject;
}
}
}
So you can see when deserializing WrapperObject, the factory is not used which I would not expect given the factory method is annotated with @JsonCreator.
This might be related to #660 and/or #2135, not sure.
The text was updated successfully, but these errors were encountered:
I am using Jackson 2.12.3 and it seems that Jackson will sometimes take preference for a private constructor over a public static factory method that is annotated with
@JsonCreator
.This example showcases the problem:
Running this (on Java 11) outputs:
So you can see when deserializing
WrapperObject
, the factory is not used which I would not expect given the factory method is annotated with@JsonCreator
.This might be related to #660 and/or #2135, not sure.
The text was updated successfully, but these errors were encountered: