Only Using @JSONignore During Serialization, But Not Deserialization

Only using @JsonIgnore during serialization, but not deserialization

Exactly how to do this depends on the version of Jackson that you're using. This changed around version 1.9, before that, you could do this by adding @JsonIgnore to the getter.

Which you've tried:

Add @JsonIgnore on the getter method only

Do this, and also add a specific @JsonProperty annotation for your JSON "password" field name to the setter method for the password on your object.

More recent versions of Jackson have added READ_ONLY and WRITE_ONLY annotation arguments for JsonProperty. So you could also do something like:

@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;

Docs can be found here.

How can you ignore a field during serialization but not during deserialization?

The javadoc of @JsonIgnore states

In addition, starting with Jackson 1.9, if this is the only annotation
associated with a property, it will also cause cause the whole
property to be ignored: that is, if setter has this annotation and
getter has no annotations, getter is also effectively ignored. It is
still possible for different accessors to use different annotations;
so if only "getter" is to be ignored, other accessors (setter or
field) would need explicit annotation to prevent ignoral (usually
JsonProperty).

So just annotate the getter and setter appropriately

// for serialization
@JsonIgnore
public String getName() {
return name;
}
// for deserialization
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}

Ignoring property when deserializing

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.

@JsonIgnore only for response but not for requests

Actually, the standard way is to have 2 separate classes for request and response, so you won't have any problem at all.

If you really need to use the same class for both cases, you can put @JsonInclude(Include.NON_NULL) onto the field instead of @JsonIgnore and set secret = null; before returning the response (as you said in question) - nullable field will be hidden after that. But it's some kind of a trick.

Selectively @JsonIgnore Immutables accessor methods, ONLY during serialization or deserialization using Jackson

I had to use the suggestion given by @benarena in this comment. However I had to explicitly specify the value attribute of the property along with the Access attribute.

@JsonProperty(value = "password", access = JsonProperty.Access.WRITE_ONLY) solved the problem.

The Immutable class would look like:

@Value.Immutable
@Value.Style(defaults = @Value.Immutable(copy = false), init = "set*")
@JsonSersonIgnoreialize(as = ImmutableUserDto.class)
@JsonDeserialize(builder = ImmutableUserDto.Builder.class)
public abstract class UserDto {

....

@JsonProperty(value = "password", access = JsonProperty.Access.WRITE_ONLY)
public abstract String getPassword();
}


Related Topics



Leave a reply



Submit