Ignoring calculated fields during deserialization

I have a class:

class MyClass {
  @Getter
  @Setter
  int a;

  @Getter
  @Setter
  int b;

  public int getADivB() {
    return a / b;
  }
}

when serializing, I need all three properties to be serialized. however, if another java process deserializes the message, I would like Jackson to ignore the computed field. (don't ignore it all with @JSONIgnore)

deserialization code:

String json = ... //get json from message
JsonNode root = this.mapper.readTree(json);
MyClass abdiv = this.mapper.readValue(root, MyClass.class);
+3
source share
3 answers

You need to annotate the calculated property using @JsonProperty so it will look like this:

class MyClass {
  @Getter
  @Setter
  int a;

  @Getter
  @Setter
  int b;

  @JsonProperty
  public int getADivB() {
    return a / b;
  }
}
+3
source

You can annotate your class with

@JsonIgnoreProperties(ignoreUnknown = true)

to ignore the Jackson property during deserialization.

+2
source

a/b ,

0

All Articles