Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}

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 Jackson 1.9.2.

like image 665
user2957698 Avatar asked Mar 27 '14 13:03

user2957698


People also ask

What is the use of Jackson ObjectMapper?

ObjectMapper is the main actor class of Jackson library. ObjectMapper class ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model (JsonNode), as well as related functionality for performing conversions.

What does ObjectMapper readValue do?

The simple readValue API of the ObjectMapper is a good entry point. We can use it to parse or deserialize JSON content into a Java object. Also, on the writing side, we can use the writeValue API to serialize any Java object as JSON output.

Does Jackson ObjectMapper use reflection?

Without any annotations, the Jackson ObjectMapper uses reflection to do the POJO mapping. Because of the reflection, it works on all fields regardless of the access modifier. If there are getters and setters available, the Jackson ObjectMapper will use that to do the mapping.

What is the use of ObjectMapper in spring boot?

Overview. When using JSON format, Spring Boot will use an ObjectMapper instance to serialize responses and deserialize requests. In this tutorial, we'll take a look at the most common ways to configure the serialization and deserialization options. To learn more about Jackson, be sure to check out our Jackson tutorial.


1 Answers

Note the javadoc of ACCEPT_EMPTY_STRING_AS_NULL_OBJECT

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

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.

You cannot use that here.

ACCEPT_EMPTY_STRING_AS_NULL_OBJECT would work for something like

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

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

class Phone {
    private String number;
}

Then deserializing would make the phone field be null.

like image 170
Sotirios Delimanolis Avatar answered Nov 15 '22 01:11

Sotirios Delimanolis