Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring property when deserializing

Tags:

java

json

jackson

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.

like image 355
Nishant Nagwani Avatar asked Apr 15 '13 16:04

Nishant Nagwani


People also ask

How do I ignore properties in JSON?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.

How do I ignore properties in ObjectMapper?

ObjectMapper; ObjectMapper objectMapper = new ObjectMapper(); objectMapper. configure(DeserializationFeature. FAIL_ON_UNKNOWN_PROPERTIES, false); This will now ignore unknown properties for any JSON it's going to parse, You should only use this option if you can't annotate a class with @JsonIgnoreProperties annotation.

How do I ignore a field in JSON request?

The Jackson @JsonIgnore annotation can be used to ignore a certain property or field of a Java object. The property can be ignored both when reading JSON into Java objects and when writing Java objects into JSON.


5 Answers

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

like image 195
kmek Avatar answered Oct 11 '22 06:10

kmek


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.

like image 21
Perception Avatar answered Oct 11 '22 05:10

Perception


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);
like image 37
Ruwanka Madhushan Avatar answered Oct 11 '22 05:10

Ruwanka Madhushan


With Jackson 2.10 you can implement a read-only field like this:

A real field

public class UserAccount implements HasMoney {

   @JsonProperty(access = JsonProperty.Access.READ_ONLY)
   private Money money;
   
   // getter and setter

}

A virtual field

@JsonIgnoreProperties(ignoreUnknown = true) // to ignore ALL unknown properties
// OR
@JsonIgnoreProperties(value = {"money"}, allowGetters = true) // to ignore only 'money' input
public class UserAccount implements HasMoney {

   @JsonProperty
   public Money getMoney() {
       // some calculation
   }

}

The value will be serialized but ignored during deserialization.

like image 27
Sergey Nemchinov Avatar answered Oct 11 '22 05:10

Sergey Nemchinov


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;
       }
    }
});
like image 41
tyoma17 Avatar answered Oct 11 '22 06:10

tyoma17