Ignoring property when deserializing
I have a simple interface with getter and setter for a property.
public interface HasMoney {
Money getMoney();
void setMoney(Money money);
}
I have another class UserAccount which implements this interface.
public class UserAccount implements HasMoney {
private Money money;
@Override
Money getMoney() // fill in the blanks
@Override
void setMoney(Money money) // fill in the blanks
}
My problem is that I want to serialize the money property but ignore while deserializing it i.e., dont accept any values from the user for this property. I have tried @JsonIgnore on setter and @JsonIgnore(false) on the getter, it does ignore it but it does so while serializing it also.
I tried @JsonIgnore on the setter and @JsonProperty on the getter just to explicitly tell Jackson that we intend to track this property, that seems to crash the application when money property is sent to the server and Jackson tries to deserialize it throwing up MalformedJsonException : cannot construct object of type Money.
The most wierd thing is that putting @JsonIgnore on the setter and @JsonProperty on the setter works for most cases when the property is primitive.
Solution 1:
Version 2.6.0+ allows this to be done with @JsonIgnoreProperties at the class level.
@JsonIgnoreProperties(value={ "money" }, allowGetters=true)
Take a look at this closed issue: https://github.com/FasterXML/jackson-databind/issues/95
Solution 2:
Ok, so the behavior of @JsonIgnore was radically changed from 1.9 onwards (for the worse imo). Without going into the devilish details of why your property is not being ignore during deserialization, try this code to fix it:
public class UserAccount implements HasMoney {
@JsonIgnore
private BigDecimal money;
// Other variable declarations, constructors
@Override
@JsonProperty
public BigDecimal getMoney() {
return money;
}
@JsonIgnore
@Override
public void setMoney(final BigDecimal money) {
this.money = money;
}
// Other getters/setters
}
Note the use of @JsonIgnore
on the field - its required for a working solution.
Note: depending on your environment and use case, you may need additional configuration on your ObjectMapper instance, for example, USE_GETTERS_AS_SETTERS, AUTO_DETECT_GETTERS, AUTO_DETECT_SETTERS etc.
Solution 3:
In a case of you don't own or can't change the class by adding @JsonIgnore
annotation, you would get expected result by using mixin starting from version 2.5 in your implementation.
public abstract class HasMoneyMixin {
@JsonIgnore
public abstract Money getMoney();
}
configure mapper to use mixin,
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(HasMoney.class, HasMoneyMixin.class);
// avoid failing if all properties are empty
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
Solution 4:
Apparently my solution is late, but definitely will be helpful for others.
Prehistory: in my project there is a class reading a JSON string directly into an entity. The JSON contains a property which is not a variable of the class. Because of
objectMapper.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
an instance of the entity will not be created during deserialization (an exception in such a case is desired in our project).
Solution:
objectMapper.addHandler(new DeserializationProblemHandler() {
@Override
public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer<?> deserializer, Object beanOrClass, String propertyName) throws IOException {
if( (propertyName.equals("propertyToBeIgnored") && beanOrClass.getClass().equals(ClassOfTheProperty.class)) {
p.skipChildren();
return true;
} else {
return false;
}
}
});