2017-02-09 88 views
3

是否有一個Jackson批註允許將數組反序列化到POJO的特定字段中?我可以用一個自定義的反序列化器很容易地做到這一點,但我希望能夠在課堂上完成這項工作。Jackson將數組元素反序列化爲特定字段

例如,我從Elasticsearch回來了以下JSON。

{ 
    "_index": "twitter", 
    "_type": "tweet", 
    "_id": "AVodOsgk0etILSbJamY-", 
    "_score": null, 
    "_source": { 
     "tweetText": "I'm at Residencial Nova Alegria https:\/\/t.co\/4TyK8PAWzB", 
     "placeCountry": "Brasil", 
     "screenName": "wildelson", 
     "cleanedText": "I'm at Residencial Nova Alegria https:\/\/t.co\/4TyK8PAWzB", 
     "resolvedUrls": [ 
     "https:\/\/www.swarmapp.com\/c\/gfULJdQ6umw" 
     ], 
     "tweetId": "829272906291085314", 
     "tweetDate": 1486549042000 
    }, 
    "sort": [ 
     1486549042000, 
     "tweet#AVodOsgk0etILSbJamY-" 
    ] 
    } 

我POJO如下:

import com.fasterxml.jackson.annotation.JsonInclude; 
import com.fasterxml.jackson.annotation.JsonProperty; 
import com.fasterxml.jackson.annotation.JsonPropertyOrder; 
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 
import lombok.Data; 

@Data 
@JsonInclude(JsonInclude.Include.NON_NULL) 
@JsonPropertyOrder(value = { 
    "enrichedStatus", 
    "sortValue", 
    "uid" 
}) 
public class TweetHit { 
    private final EnrichedStatus enrichedStatus; 
    private final Long sortValue; 
    private final String uid; 

    public TweetHit(EnrichedStatus enrichedStatus, Long sortValue, String uid) { 
     this.enrichedStatus = enrichedStatus; 
     this.sortValue = sortValue; 
     this.uid = uid; 
    } 

我想要的「排序」陣列,這將總是有一個很長的陣列[0]和在陣列的字符串[1],待反序列化如下:

Long sortValue = sort[0] 
String uid = sort[1] 

我發現一個問題要問,這裏唯一的答案是一個自定義解串器,這是我想避免,如果我能。 Jackson: Deserialize JSON-Array in different attributes of an object

我想也許我可以使用@JsonProperty("sort")@JsonFormat(shape = ARRAY)莫名其妙地,但我沒有看到反正指定特定單元格反序列化到每個字段。

請注意,這是一個不可變的對象,所以我可能需要解決方案與構造函數一致工作,雖然也許我可以刪除final,添加一個空的構造函數,將註釋放在字段級別並使用Lombok如果傑克遜可以直接設置字段,則禁用setters?

回答

1

您可以通過@JsonCreator@JsonProperty註釋和自定義構造函數來處理此問題。因爲我不知道什麼class EnrichedStatus貌似我只用sort陣列簡化您的示例:

import java.io.IOException; 
import com.fasterxml.jackson.annotation.JsonCreator; 
import com.fasterxml.jackson.annotation.JsonProperty; 
import com.fasterxml.jackson.databind.ObjectMapper; 
import lombok.Data; 

@Data 
class TweetHit { 
    private final Long sortValue; 
    private final String uid; 

    @JsonCreator 
    public TweetHit(@JsonProperty("sort") String[] sort) { 
     this.sortValue = Long.parseLong(sort[0]); 
     this.uid = sort[1]; 
    } 
} 

public class StackOverflow { 
    public static void main(String[] args) throws IOException { 
     String json = "{ \"sort\": [\n" + 
       "  1486549042000,\n" + 
       "  \"tweet#AVodOsgk0etILSbJamY-\"\n" + 
       " ]\n" + 
       " }"; 

     ObjectMapper objectMapper = new ObjectMapper(); 
     TweetHit tweetHit = objectMapper.readValue(json, TweetHit.class); 
    } 
} 

這保留TweetHit類的不變性,避免了自定義解串器。

我看不到用@JsonFormat(shape = ARRAY)來達到這個目的的方法。即使有可能,我懷疑它比構造函數更簡單,該構造函數接受數組並將其內容分配給字段。

如果您還想避免@JsonCreator@JsonProperty註釋,請檢查this answer

+0

哇,我不敢相信我沒有看到。我有兩個領域,因此有2個參數的隧道願景。這工作。謝謝! – Brooks

相關問題