Given I have the following JSON file:
{
"cleanup": false,
"clearCache": false
}
and the following bean:
import org.apache.commons.lang3.Validate;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Config implements IHoldConfiguration {
public static final boolean DEFAULT_CLEANUP = false;
public static final boolean DEFAULT_CLEAR_CACHE = false;
public static final int DEFAULT_CONCURRENCY = 1;
@JsonProperty(value = "cleanup", required = false, defaultValue = "false")
private Boolean cleanup;
@JsonProperty(value = "clearCache", required = false, defaultValue = "false")
private Boolean clearCache;
@JsonProperty(value = "concurrency", required = false, defaultValue = "1")
private Integer concurrency;
public boolean isCleanup() {
return this.cleanup.booleanValue();
}
public void setCleanup(final Boolean cleanup) {
this.cleanup = cleanup != null ? cleanup : DEFAULT_CLEANUP;
}
public boolean isClearCache() {
return this.clearCache.booleanValue();
}
public void setClearCache(final Boolean clearCache) {
this.clearCache = clearCache != null ? clearCache : DEFAULT_CLEAR_CACHE;
}
public int getConcurrency() {
return this.concurrency.intValue();
}
public void setConcurrency(Integer concurrency) {
if (concurrency == null) {
concurrency = DEFAULT_CONCURRENCY;
} else {
Validate.inclusiveBetween(1, Integer.MAX_VALUE, concurrency,
String.format("concurrency must be in range [1, %s]", Integer.MAX_VALUE));
}
this.concurrency = concurrency;
}
}
How do I force Jackson 2.9.2 to use my concurrency
setter? Currently, when deserialized from JSON to bean, using new ObjectMapper().readValue(inputStream, Config.class)
, concurrency
is set to null
.
I thought that Jackson used the setters in a bean, if provided, to set a property. After seeing the deserialized result, and debugging, I've found this is not true. Since the defaultValue
aatribute of JsonProperty
is for documentation only, I thought I could add default value setting in the setters, but obviously this did not work. How can I accomplish this?