1
次のjsonを非直列化してJava pojoにします。デシリアライズ時に空の文字列をnullとして無視します。
[{
"image" : {
"url" : "http://foo.bar"
}
}, {
"image" : "" <-- This is some funky null replacement
}, {
"image" : null <-- This is the expected null value (Never happens in that API for images though)
}]
そして、私のJavaクラスのようになります。私はジャクソン2.8.6
ObjectMapper.read(json, LIST_OF_SERVER_TYPE_REFERENCE);
を使用しますが、私は次の例外を取得しておく
public class Server {
public Image image;
// lots of other attributes
}
と
public class Image {
public String url;
// few other attributes
}
:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of Image: no String-argument constructor/factory method to deserialize from String value ('')
私はそれ
public void setImage(Image image) {
this.image = image;
}
public void setImage(String value) {
// Ignore
}
のための文字列のセッターを追加した場合、私は次の例外
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
例外は(も)かどうか、私は変更されません取得するイメージセッターを追加したり、ない。
私も@JsonInclude(NOT_EMPTY)
を試しましたが、これは唯一のシリアル化に影響するようです。
概要:いくつかの(悪い設計された)API私の代わりにnull
の空の文字列(""
)を送信し、私はちょうどその不正な値を無視するようにジャクソンを伝える必要があります。これどうやってするの?
を使用して、それを使用デシリアライザは、イメージオブジェクトまたは文字列を持っているかどうかを最初にチェックしてから、それを逆シリアル化します。 – JMax