简体   繁体   English

Jackson 无法识别“是”字段,除非使用 @JsonProperty 进行注释

[英]Jackson not recognizing 'is' Fields unless annoated with @JsonProperty

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.每当我的 JSON 字符串 Jackson 中有一个“是”字段时,如果没有额外的 @JsonProperty 标签,我就无法检测到它,我发现了以下问题。 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 ( ). UnmappedPropertyHandler接口只是一个带有 @JsonAnySetter 的方法,用于报告任何未知字段,只要它们没有用 @JsonProperty 注释并且它们的名称在 ( ) 之间,它就会对“是”字段执行此操作。

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.我正在解析的 JSON 字符串的片段。

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

Jackson apparently scans the class that you pass in for getters and setters and then use those methods for serialization and deserialization. Jackson 显然会扫描您传入的 class 获取 getter 和 setter,然后使用这些方法进行序列化和反序列化。

"Get", "Set", "is" are eliminated and what remains in those methods will be used as Json field. “Get”、“Set”、“is”被删除,这些方法中剩余的内容将用作 Json 字段。

Hence your "isCommunity" is changed into "community" and so on.因此你的“isCommunity”变成了“community”等等。

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.默认情况下, Jackson 将读取以getset开头的方法,因此您必须调用方法getIsCommunitysetIsCommunity ,它会将 map 的值传递给正确的字段,尽管我不确定它是否是比注释更好的解决方案.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM