Java Jackson ObjectMapper DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22688713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:18:21  来源:igfitidea点击:

Hymanson ObjectMapper DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT

javajsonHymansonjson-deserialization

提问by user2957698

I am using Hymanson 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

我在 Jersey 应用程序中使用 Hymanson 进行 JSON 序列化/反序列化。我想在我的 java POJO 属性中将 JSON 中的空字符串读取为空值。我试图在对象映射器上设置 DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT 但它不起作用。这是下面的代码

import java.io.IOException;
import org.codehaus.Hymanson.JsonParseException;
import org.codehaus.Hymanson.map.DeserializationConfig;
import org.codehaus.Hymanson.map.JsonMappingException;
import org.codehaus.Hymanson.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();
        }
    }

}

The bean is as below

豆子如下

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();
    }


}

The output is always

输出总是

Phone Number is empty string

I am not sure why is this not working. I am using Hymanson 1.9.2.

我不确定为什么这不起作用。我正在使用Hyman逊 1.9.2。

回答by Sotirios Delimanolis

Note the javadoc of ACCEPT_EMPTY_STRING_AS_NULL_OBJECT

注意javadoc ACCEPT_EMPTY_STRING_AS_NULL_OBJECT

Feature that can be enabled to allow JSON empty String value ("") to be bound to POJOs as null.

可以启用以允许 JSON 空字符串值 ("") 绑定到 POJO 为 null 的功能。

So an empty Stringcan be used to map nullto a POJO. A Stringis not a POJO, it is considered a JSON primitive value.

所以String可以使用空来映射null到 POJO。AString不是 POJO,它被认为是 JSON 原始值。

You cannot use that here.

你不能在这里使用它。

ACCEPT_EMPTY_STRING_AS_NULL_OBJECTwould work for something like

ACCEPT_EMPTY_STRING_AS_NULL_OBJECT会为类似的东西工作

{"name":"First Name","phone":"","unknown":"test"}

Where you would make the TestBean.phonefield of some POJO type like

您可以在哪里制作TestBean.phone某些 POJO 类型的字段,例如

class Phone {
    private String number;
}

Then deserializing would make the phonefield be null.

然后反序列化将使该phone字段成为null.