FasterXML / FasterXML/jackson-annotations

Property is not serialized when both @JsonProperty and @JsonIgnore are present

未关闭
#305 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
documentation
主要语言
Java
星标
1.1k
派生
342
平均合并
8 小时 53 分钟
30 天内合并 PR
2

描述

I encountered a situation where a property annotated with both `@JsonProperty` and `@JsonIgnore` is not being serialized. In [Jackson wiki](https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations) said, that `@JsonProperty`:

> also indicates that property is to be included

Based on this, I assumed that `@JsonProperty` would override `@JsonIgnore`. However, in the following example, the property is omitted from the serialized JSON:

```java
public class Box {
@JsonProperty
@JsonIgnore
private int size;

public Box(int size) {
this.size = size;
}

public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
Box box = new Box(10);

String json = objectMapper.writeValueAsString(box);
System.out.println(json);
}
}
```
Expected output:
```json
{"size":10}
```
But got:
```json
{}
```
**Jackson version**: `2.19.3`

I used a very simple example just to demonstrate the behavior, but the same issue also occurs with subclasses.

If this behavior is expected, it would be very helpful to have documentation describing the priority of different annotations, because it is currently unclear which annotation takes precedence over others.

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。