0

I'm pretty new to using Jackson and I'm trying to follow my team's patterns for deserializing our JSON. Right now I am running into an issue when a field name doesn't match the JSON property.

Working example:

@JsonDeserialize(builder = ProfilePrimaryData.Builder.class)
@Value
@ParametersAreNonnullByDefault
@Builder(builderClassName = "Builder")
private static class ProfilePrimaryData {
    private final Boolean hasProfile;

    @JsonPOJOBuilder(withPrefix = "")
    public static class Builder {
    }
}

If the JSON property is hasProfile, it works fine, but if it's has_profile (which is what our client is writing), it doesn't work and I get an error: Unrecognized field "has_profile" (class com.mypackagehere.something.$ProfilePrimaryData$Builder), not marked as ignorable (one known property: "hasProfile"]). I've tried adding a JsonProperty annotation to hasProfile like this but it still doesn't work:

@JsonDeserialize(builder = ProfilePrimaryData.Builder.class)
@Value
@ParametersAreNonnullByDefault
@Builder(builderClassName = "Builder")
private static class ProfilePrimaryData {
    @JsonProperty("has_profile")
    private final Boolean hasProfile;

    @JsonPOJOBuilder(withPrefix = "")
    public static class Builder {
    }
}

Am I misunderstanding how this is supposed to work?

CustardBun
  • 3,457
  • 8
  • 39
  • 65
  • This might help you - https://stackoverflow.com/questions/16019834/ignoring-property-when-deserializing or `@JsonIgnoreProperties(value = { "has_profile" })` class level annotation or to be more generic `@JsonIgnoreProperties(ignoreUnknown = true)` – SASIKUMAR SENTHILNATHAN Jun 11 '18 at 23:44
  • @SASIKUMARS - I don't think we're on the same page - I want the has_profile field to be read in and put into the hasProfile field in the POJO. Wouldn't JsonIgnoreProperties just throw the value away? – CustardBun Jun 11 '18 at 23:47
  • can you post the sample JSON? – SASIKUMAR SENTHILNATHAN Jun 12 '18 at 00:38

1 Answers1

2

Error clearly says that Unrecognized field "has_profile" (class com.mypackagehere.something.$ProfilePrimaryData$Builder)
i.e has_profile is missing from Builder class, not ProfilePrimaryData class, so you have to annotate Builder class properties.

@JsonDeserialize(builder = ProfilePrimaryData.Builder.class)
public class ProfilePrimaryData {

    /*
     * This annotation only needed, if you want to
     * serialize this field as has_profile,
     * 
     * <pre>
     * with annotation
     * {"has_profile":true}
     * 
     * without annotation
     * {"hasProfile":true}
     * <pre>
     *  
     */
    //@JsonProperty("has_profile")
    private final Boolean hasProfile;

    private ProfilePrimaryData(Boolean hasProfile) {
        this.hasProfile = hasProfile;
    }

    public Boolean getHasProfile() {
        return hasProfile;
    }

    @JsonPOJOBuilder(withPrefix = "")
    public static class Builder {

        // this annotation is required
        @JsonProperty("has_profile")
        private Boolean hasProfile;

        public Builder hasProfile(Boolean hasProfile) {
            this.hasProfile = hasProfile;
            return this;
        }

        public ProfilePrimaryData build() {
            return new ProfilePrimaryData(hasProfile);
        }
    }
}
Hemant Patel
  • 3,160
  • 1
  • 20
  • 29