FasterXML / jackson-annotations

Core annotations (annotations that only depend on jackson-core) for Jackson data processor
https://github.com/FasterXML/jackson
Apache License 2.0
1.03k stars 330 forks source link

@JsonProperty on a boolean field results into two fields in the final JSON #220

Closed Denis-D-M closed 1 year ago

Denis-D-M commented 1 year ago

Hey guys, sorry to bother, but I came across with this strange behavior:

Code:

  @JsonProperty("IsPrimary")
  private Boolean isPrimary;

    public Boolean getPrimary() {
    return isPrimary;
  }

  public void setPrimary(Boolean primary) {
    isPrimary = primary;
  }

Result:

{
  "primary": true,
  "IsPrimary": true,
}
pjfanning commented 1 year ago

try adding @JsonIgnore to getPrimary()

cowtowncoder commented 1 year ago

@Denis-D-M It is a feature, not bug: neither initial names nor override match, so they are considered to represent different property (properties are not required to relate to both Field and Method etc).

But depending on what exactly you are trying to achieve (as in, whether to serialize value of Field directly, or via getter) you need to change annotations. Either, like @pjfanning suggested, add @JsonIgnore or, maybe more likely, more @JsonProperty to get method (in which case private field is just ignored).