Skip to content
Advertisement

Different @JsonProperty config for READ and WRITE on same field?

I have a class (which cannot be modified) like

JavaScript

When deserializing I get data like:

JavaScript

As I cannot modify the Standing class I have a mix-in like:

JavaScript

As the received json does not have positionNumber and positionText fields I use the @JsonPropery annotations.

With Access.READ_ONLY I simply ignore the positionNumber field.

And with @JsonProperty(value = "position", access = JsonProperty.Access.WRITE_ONLY) on the positionText field I make sure it’s populated with the position field from the json during deserialization.

This works well during deserialization.

Note the StandingSanitizer sets the positionNumber. This as the received position value can be non-number values like DSQ in which case the positionNumber field will be null.

But when serializing I want to output all 3 fields from the Standing class like:

JavaScript

But because of @JsonProperty(value = "position", access = JsonProperty.Access.WRITE_ONLY) on the positionText field it is not serialized unfortunately.

In theory I would like to do have something like:

JavaScript

where I could use different @JsonProperty annotation for both READ and WRITE.

But this is not possible as duplicate @JsonProperty annotations on a field are not allowed; and as far as I could see there is no support for repeatable annotations.

Is there any other solution to solve this?

One thing I can think of is to have 2 ObjectMapper instances, with 2 different StandingMixIns; 1 for deserializing and 1 for serializing. But I would prefer to keep having 1 ObjectMapper instance, so using 2 would be a last resort.

Advertisement

Answer

You could use the getters and setters to get that extra customization. The get will act as READ and the set as WRITE. Note that you don’t need the access properties or the field level annotation:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement