Skip to content

@JsonIgnore does not if together with @JsonProperty or @JsonFormat #3357

Closed
@lizongbo

Description

@lizongbo

Describe the bug
JsonIgnore is not work when with JsonProperty or JsonFormat。
code:

    @JsonIgnore
    @JsonProperty("create_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    Date createTime;

Version information
2.13.1

To Reproduce
test case:

@Test
void testJsonIgnore() throws JsonProcessingException {
    TestIgnorebean tb = new TestIgnorebean();
    tb.setCreateTime(new Date());
    tb.setAge(10);
    tb.setNickName("lizongbo");
    ObjectMapper mapper = new ObjectMapper();
    JsonInclude.Value full = JsonInclude.Value.construct(Include.ALWAYS, Include.ALWAYS);
    mapper.setDefaultPropertyInclusion(full);
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    mapper.enable(Feature.WRITE_BIGDECIMAL_AS_PLAIN);
    mapper.setTimeZone(TimeZone.getDefault());
    System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(tb));
    System.out.println("jackson vesion " + JsonIgnore.class.getPackage().getImplementationVersion());
}

static class TestIgnorebean {
    @JsonIgnore
    @JsonProperty("nick_name")
    String nickName;
    @JsonIgnore
    @JsonProperty("create_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    Date createTime;
    int age;

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

result:

{
  "nickName" : "lizongbo",
  "createTime" : 1640089093927,
  "age" : 10
}

jackson vesion 2.13.1

// JsonIgnore, JsonFormat and JsonProperty all not work correct.

Expected behavior
need:

{
  "age" : 10
}

jackson vesion 2.13.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    has-failing-testIndicates that there exists a test case (under `failing/`) to reproduce the issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions