Skip to content

Make FAIL_ON_NULL_FOR_PRIMITIVES apply to primitive arrays and other types that wrap primitives #403

@harleensahni

Description

@harleensahni

DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES only seems to work on primitive properties on beans. It doesn't seem to work on reading straight values (I don't care that much about this), and reading primitive arrays.

In addition, it would be good if this functionality, or a new DeserializationFeature was added to support failing on nulls in Lists that store wrap primitive types since people often use List as a property type instead of int[] for convenience.

Here is a test case I wrote to demonstrate this:

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class TestNullPrimitives {

    public static class PrimitiveTest{
        public int value;
        public int[] valueArray;
        public List<Integer> valueList;
    }

    @Test(expected = JsonMappingException.class)
    public void testReadNullInt() throws IOException {

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);
        // Reads value as 0
        int value  = mapper.readValue("null", int.class);
        System.out.println("value:" + value);

    }

    @Test(expected = JsonMappingException.class)
    public void testObjectReadNullProperty() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);
        // throws exception
        PrimitiveTest testObject = mapper.readValue("{\"value\":null}", PrimitiveTest.class);
    }
    @Test(expected = JsonMappingException.class)
    public void testObjectReadNullArrayEntryInProperty() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);
        // Reads valueArray as [0]
        PrimitiveTest testObject = mapper.readValue("{\"valueArray\":[null]}", PrimitiveTest.class);
        System.out.println("Contents:" + Arrays.toString(testObject.valueArray));
        System.out.println("Size:" + testObject.valueArray.length);
    }

    @Test(expected = JsonMappingException.class)
    public void testObjectReadNullListEntryInProperty() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);
        // Reads valueList as [null]
        PrimitiveTest testObject = mapper.readValue("{\"valueList\":[null]}", PrimitiveTest.class);
        System.out.println("Contents:" + testObject.valueList);
        System.out.println("Size:" + testObject.valueList.size());
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions