Make FAIL_ON_NULL_FOR_PRIMITIVES apply to primitive arrays and other types that wrap primitives
See original GitHub issueDeserializationFeature.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<Integer> 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());
}
}
Issue Analytics
- State:
- Created 10 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Converting Array of Primitives to Array of Containers in Java
All methods called toObject(...) convert from primitive array to wrapper array; All called toPrimitive(...) convert from wrapper object ...
Read more >Array Initialization | Java Operators with Primitives and Objects
This sample chapter helps you prepare for Exam 310-035 by reviewing the way Java uses literals to initialize primitive variables, create ...
Read more >Convert an Array of Primitives to an Array of Objects | Baeldung
Let's see how we can use autoboxing in an iteration. First, let's convert from a primitive array to an object array: int[] input...
Read more >Converting Between Array of Primitives to Array of Objects
First, we will convert an array of primitive types to an array of wrapper objects. To do that, we will simply iterate through...
Read more >Autoboxing and Unboxing
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@shmosel I try to update issues with notes when there is progress so no updates. But as luck would have it I did work in related area for
2.9.0.pr1
so I hope to have a look at this relatively soon (for 2.9.0.pr2 at least – not sure how safe behavioral change this would be for 2.8)Implemented for 2.9.0(.pr2), added tests: should cover both coercion from literal
null
and empty String, similar to how it works for primitive-valued POJO properties.