Jackson ObjectMapper DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT [英] Jackson ObjectMapper DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT
问题描述
我在Jersey应用程序中使用Jackson进行JSON序列化/反序列化。我想在我的java POJO属性中将JSON中的空字符串读取为null值。我试图在Object Mapper上设置DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT但它不起作用。以下是代码
I am using Jackson for JSON serialization/deserialization in my Jersey application. I want to read the empty strings in JSON to null value in my java POJO property. I tried to set the DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT on Object Mapper but it is not working. Here is the code below
import java.io.IOException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class TestMapper {
/**
* @param args
*/
public static void main(String[] args) {
TestMapper.testJson();
}
public static void testJson(){
String jsonString = "{\"name\":\"First Name\",\"phone\":\"\",\"unknown\":\"test\"}";
ObjectMapper result = new ObjectMapper();
//result.setDeserializationConfig(result.getDeserializationConfig().with(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT));
//result.enable(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
/*DeserializationConfig deserializeConfig = result.getDeserializationConfig();
deserializeConfig.with(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);*/
result.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
result.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
try {
TestBean testBean = result.readValue(jsonString, TestBean.class);
if(testBean.getPhone()!=null&& testBean.getPhone().equals("")){
System.out.print("Phone Number is empty string");
}else{
System.out.print("Phone Number is null");
}
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
豆子如下所示
public class TestBean {
private String name;
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public TestBean(String name, String phone) {
super();
this.name = name;
this.phone = phone;
}
public TestBean() {
super();
}
}
输出是总是
Phone Number is empty string
我不知道为什么这不起作用。我正在使用Jackson 1.9.2。
I am not sure why is this not working. I am using Jackson 1.9.2.
推荐答案
注意 ACCEPT_EMPTY_STRING_AS_NULL_OBJECT
可以启用的功能允许JSON空字符串值()绑定到POJO为空。
Feature that can be enabled to allow JSON empty String value ("") to be bound to POJOs as null.
因此可以使用空字符串
来映射 null
到POJO。 字符串
不是POJO,它被认为是JSON原始值。
So an empty String
can be used to map null
to a POJO. A String
is not a POJO, it is considered a JSON primitive value.
你不能在这里使用它。
ACCEPT_EMPTY_STRING_AS_NULL_OBJECT
可用于类似
{"name":"First Name","phone":"","unknown":"test"}
你在哪里制作一些POJO类型的 TestBean.phone
字段,如
Where you would make the TestBean.phone
field of some POJO type like
class Phone {
private String number;
}
然后反序列化会使手机
字段为 null
。
Then deserializing would make the phone
field be null
.
这篇关于Jackson ObjectMapper DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!