我要将Json null值反序列化为Java Object空字符串
我可以创建自定义反序列化器,但是当Json值为null时,它不会进入反序列化器。
我该如何反序列化呢?
提前致谢!
public class CustomStringDeserializer extends JsonDeserializer<String> {
@Override
public String deserialize(JsonParser jsonparser, DeserializationContext deserializationcontext) throws IOException,
JsonProcessingException {
String str = jsonparser.getText();
try {
return (str == null) ? "" : str;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public CustomObjectMapper() {
SimpleModule _module = new SimpleModule("Module", new Version(1, 9, 10, "FINAL"));
_module.addDeserializer(String.class, new CustomStringDeserializer());
}
谢谢@nutlike
我这样做是
@Override
public String getNullValue() {
return "";
}
答案 0 :(得分:2)
覆盖方法getNullValue()也许就足够了?
public class CustomStringDeserializer extends JsonDeserializer<String> {
@Override
public String deserialize(JsonParser jsonparser,
DeserializationContext deserializationcontext) throws IOException,
JsonProcessingException {
return jsonparser.getText();
}
@Override
public String getNullValue() {
return "";
}
}