3

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 is deserializing the message i would like jackson to ignore the calculated field. (not ignore it all together as with @JSONIgnore)

deserialization code is:

String json = ... //get json from message
JsonNode root = this.mapper.readTree(json);
MyClass abdiv = this.mapper.readValue(root, MyClass.class);
1
  • 2
    I only see two fields, where is the third?
    – esej
    May 10, 2012 at 21:45

3 Answers 3

3

What you need is to annotate calculated property with @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

You can annotate your class with

@JsonIgnoreProperties(ignoreUnknown = true)

to have the property ignored by Jackson during deserialization.

0

serialization will not serialize the a/b in the method, only the member fields

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.