virtualpathum
virtualpathum

Reputation: 763

Is there any way to prevent field from deserialization in jackson?

Is there any way to prevent a field from deserialization in jackson? but i need to serialize that field I tried with @jsonIgnoreProperties this prevent both serialization and deserialization.

Upvotes: 2

Views: 4196

Answers (3)

Frank Pavageau
Frank Pavageau

Reputation: 11735

Starting with Jackson 2.6, a property can be marked as read- or write-only. It's simpler than hacking the annotations on both accessors and keeps all the information in one place:

public class NoDeserialization {
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private String prop;

    public String getProp() {
        return prop;
    }

    public void setProp(String prop) {
        this.prop = prop;
    }
}

Upvotes: 4

user3145373 ツ
user3145373 ツ

Reputation: 8156

You can use @JsonIgnore om field declaration.

ex.

@JsonIgnore
private Integer fieldToPrevent; // this will be avoided

private Integer regularField; // will serialize

look here's a explanation of it.

I have gave same answer here also.

Upvotes: 0

Master Slave
Master Slave

Reputation: 28569

The "trick" is to combine the @JsonProperty and @JsonIgnore on the setters and getters, like in the following example

 public class SerializeDemo{

      @JsonIgnore
      private String serializeOnly;


      @JsonProperty("serializeOnly")
      public String getSerializeOnly() {
        return serializeOnly;
      }

      @JsonIgnore
      public void setSerializeOnly(String serializeOnly) {
        this.serializeOnly= serializeOnly;
      }
    }

Upvotes: 3

Related Questions