How to enable jackson to store a computed field on a serialized class

324 Views Asked by At

Similar to this stackoverflow thread Jackson with JSON: Unrecognized field, not marked as ignorable

I am getting com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field error.

I don't want to have to save this value in a field on the class. I want it to be computed when it is being serialized. And possibly ignored when it is being deserialized (because then I can just recompute it)

I think this may have to do with the fact that these methods are never called. Because other computed methods which being called are being serialized just fine.

1

There are 1 best solutions below

1
Askin Geeks On

If the method returns a value computed from other properties, make sure it is ignored by Jackson by adding @ com.fasterxml.jackson.annotation.JsonIgnore to the method.

Alternatively - if you want to serialize the value and ignore it/not fail on deserialization then there are two options I know of:

  • add @JsonIgnoreProperties(ignoreUnknown=true) to the class
  • or call mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) on the ObjectMapper you are using for deserialization.