使用具有字段依赖性的Jackson反序列化JSON

时间:2016-11-21 04:53:31

标签: java android json jackson

在我们的项目中,我们用杰克逊解析JSON。我们按字段saved设置字段channelId。问题是channelId字段的解析晚于saved。因此,当我们想要设置字段saved字段channelId时为空。我们如何在JSON反序列化中设置字段依赖,因此字段saved将在channelId之后设置?

这是我们的JSON数据的一部分:

"message":{  
            "data":{  
               "text":"Some text"
            },
            "saved_by":[  
               2715,
               1234
            ],
            "some_boolean_field":false,
            "channel_id":8162
           }

这是我们的实体类:

@JsonIgnoreProperties(ignoreUnknown = true)
@org.parceler.Parcel(org.parceler.Parcel.Serialization.BEAN)

public class Message {

    @JsonProperty("channel_id")
    protected long channelId;

    protected boolean saved;

    @JsonSetter("saved_by")
    public void setSavedBy(Set<Long> savedBy) {
        saved = savedBy.contains(getUserIdByChannelId(channelId));
    }

    public long getChannelId() {
        return channelId;
    }

    public void setChannelId(long channelId) {
        this.channelId = channelId;
    }

    public boolean isSaved() {
        return saved;
    }

    public void setSaved(boolean saved) {
        this.saved = saved;
    }

    public void setData(JsonNode data) throws JsonProcessingException {
        JsonNode textNode = data.get("text");
        text = textNode != null ? textNode.asText() : "";

        components = new ArrayList<>();
        JsonNode mediaNode = data.get("media");
        if (mediaNode != null) {
            MessageComponent[] parsedComponents = AppSession.getInstance().getObjectMapper().treeToValue(mediaNode, MessageComponent[].class);
            List<MessageComponent> components = Arrays.asList(parsedComponents).subList(0, parsedComponents.length < 4 ? parsedComponents.length : 4);

            this.components.addAll(components);
        }

        mediaCount = components.size();
    }

    }

Full JSON:

{  
   "data":{  
      "serial":66,
      "updated_entity":"bookmark",
      "bookmark":{  
         "message":{  
            "data":{  
               "text":"hello"
            },
            "counted_serial":748,
            "saved_by":[  
               26526,
               27758
            ],
            "type":"UserMessage",
            "is_reviewed":false,
            "channel_id":8128,
            "id":2841531,
            "replied_message_data":null,
            "is_blocked":false,
            "is_deleted":false,
            "updated_at":"2016-11-21T05:59:52.471Z",
            "spam_reported_by":[  

            ],
            "created_at":"2016-11-19T15:40:17.027Z",
            "uuid":"0b6ba58e-f5e1-4ee5-a9da-041dfc2c85cd",
            "liked_by":[  

            ],
            "user":{  
               "last_name":"M",
               "id":4537,
               "first_name":"John",
               "is_deleted":false,
               "avatar_thumb":"https:\/\/cdn.site.org\/uploads\/99ef4d68-6eaf-4ba6-aafa-74d1cf895d71\/thumb.jpg"
            },
            "serial":934
         },
         "id":6931,
         "created_at":"2016-11-21T05:59:52.459Z",
         "is_deleted":false,
         "updated_at":"2016-11-21T05:59:52.459Z"
      }
   },
   "type":"action_performed"
}

1 个答案:

答案 0 :(得分:1)

这有点像hackish,但通过使Message类成为自己的反序列化构建器,您可以获得一种“为bean创建的准备” - 可以访问所有属性的事件。

我的建议是你尝试以下方法:

@JsonDeserialize(builder = Message.class)
public class Message {

    ...

    @JsonSetter("saved_by")
    public void setSavedBy(Set<Long> savedBy) {
        // Merely store the value for later use.
        this.savedBy = savedBy;
    }

    ...

    public Message build() {
        // Calculate value of "saved" field.
        this.saved = this.savedBy.contains(getUserIdByChannelId(this.channelId));
        return this;
    }

    // Handling the added challenge.
    @JsonProperty("data") 
    public void setData(JsonNode data) throws JsonProcessingException {
       ...
    }
}

以上内容利用了JsonPOJOBuilder注释的默认设置,即buildMethodName的默认值为build