2013-04-11 35 views
0

我遇到了从Web服务返回的不稳定数据的问题。当一个对象不存在时,我可能会在回应中返回一个布尔值。杰克逊编组数据不一致

快乐路径:

{ 
    "foo": {"msg": bar} 
} 

不幸的路径:

{ 
    "foo": false 
} 

当返回一个布尔值,我想它存储为一个空Foo对象,但到目前为止,我还没有找到杰克逊解决这个问题的好方法。

大部分我迄今为止所做的只是使用@JsonProperty注释将我的对象映射到json响应。

//... Omitted code 
@JsonProperty("foo") 
public void setBar(Bar bar) { 
    this.bar = bar 
} 


class Bar { 
    String msg; 
    // ... Getter and setter below ... 
} 

回答

1

我找到了解决方案。这不是很优雅,但它不需要重写任何主要的代码。我能够在我的foo属性设置器中使用JsonNode对象。不过,我会看看我能不能做出更好的东西。

@JsonProperty("foo") 
public void setBar(JsonNode barJsonNode) { 
    if(barJsonNode.isBoolean()){ 
     bar = null; 
    } else { 
     // Magic goes here 
    } 

}