Home > Net >  Jackson not recognizing 'is' Fields unless annoated with @JsonProperty
Jackson not recognizing 'is' Fields unless annoated with @JsonProperty

Time:06-13

I have discovered the following problem whenever I have an "is" field in my JSON String Jackson doesn't detect it without the additional @JsonProperty Tag. Before reporting this as a Bug, I wanted to make sure this is not caused by me. The UnmappedPropertyHandler interface is just a method with an @JsonAnySetter to report any unknown fields, which it does for the "is" Fields whenever they are not annotated with @JsonProperty and their name between the ( ).

public class Event implements UnmappedPropertyHandler {

    private boolean expired, isCommunity, isPersonal, active;

    public boolean isExpired() {
        return expired;
    }

    public boolean isCommunity() {
        return isCommunity;
    }

    public boolean isPersonal() {
        return isPersonal;
    }

    public boolean isActive() {
        return active;
    }

Snippet of the JSON String I'm parsing.

{
  "id": "5c7cb0d00000000000000000",
  "activation": "2022-06-01T16:00:00.000Z",
  "startString": "-11d 17h 33m 47s",
  ....
  "isPersonal": true,
  "isCommunity": true,
  ....
}

CodePudding user response:

Jackson apparently scans the class that you pass in for getters and setters and then use those methods for serialization and deserialization.

"Get", "Set", "is" are eliminated and what remains in those methods will be used as Json field.

Hence your "isCommunity" is changed into "community" and so on.

CodePudding user response:

By default, Jackson will read methods starting by get or set, so you'd have to call your methods getIsCommunity and setIsCommunity, and it would map the value to the correct field, although I am not sure it is a better solution than an annotation.

  • Related