Skip to content
Advertisement

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

I have a class (which cannot be modified) like

public class Standing {

    private Integer positionNumber;
    private String positionText;
    private BigDecimal points;
    ..
}

When deserializing I get data like:

{
    "position": "1",
    "points": 10
}

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

@JsonDeserialize(converter = StandingSanitizer.class)
public abstract class StandingMixIn {

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    Integer positionNumber;

    @JsonProperty(value = "position", access = JsonProperty.Access.WRITE_ONLY)
    String positionText;
}

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:

{
    "positionText": "1",
    "positionNumber": 1,
    "points": 10
}

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:

@JsonDeserialize(converter = StandingSanitizer.class)
public abstract class StandingMixIn {

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    Integer positionNumber;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @JsonProperty(value = "position", access = JsonProperty.Access.WRITE_ONLY)
    String positionText;
}

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:

    public abstract class StandingMixIn {
        @JsonProperty(access = JsonProperty.Access.READ_ONLY)
        Integer positionNumber;
        
        // No annotation on the field
        String positionText;

        @JsonProperty(value = "positionText")
        public String getPositionText() {
            return positionText;
        }

        @JsonProperty(value = "position")
        public void setPositionText(String positionText) {
            this.positionText = positionText;
        }
    }
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement