Skip to content

Commit 8bf0c05

Browse files
committed
Fix #3421 (add JsonNodeFeature.READ_NULL_PROPERTIES)
1 parent a8a233f commit 8bf0c05

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

release-notes/VERSION-2.x

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Project: jackson-databind
88

99
#3373: Change `TypeSerializerBase` to skip `generator.writeTypePrefix()`
1010
for `null` typeId
11+
#3405: Create DataTypeFeature abstraction (for JSTEP-7) with placeholder features
12+
#3421: Implement `JsonNodeFeature.READ_NULL_PROPERTIES` to allow skipping of
13+
JSON `null` values on reading
1114

1215
2.13.3 (not yet released)
1316

src/main/java/com/fasterxml/jackson/databind/deser/std/JsonNodeDeserializer.java

+10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import java.util.Arrays;
55

66
import com.fasterxml.jackson.core.*;
7+
78
import com.fasterxml.jackson.databind.*;
9+
import com.fasterxml.jackson.databind.cfg.JsonNodeFeature;
810
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
911
import com.fasterxml.jackson.databind.node.*;
1012
import com.fasterxml.jackson.databind.type.LogicalType;
@@ -402,6 +404,10 @@ protected final JsonNode updateObject(JsonParser p, DeserializationContext ctxt,
402404
value = nodeFactory.booleanNode(false);
403405
break;
404406
case JsonTokenId.ID_NULL:
407+
// 20-Mar-2022, tatu: [databind#3421] Allow skipping `null`s from JSON
408+
if (!ctxt.isEnabled(JsonNodeFeature.READ_NULL_PROPERTIES)) {
409+
continue;
410+
}
405411
value = nodeFactory.nullNode();
406412
break;
407413
default:
@@ -485,6 +491,10 @@ protected final ContainerNode<?> _deserializeContainerNoRecursion(JsonParser p,
485491
value = nodeFactory.booleanNode(false);
486492
break;
487493
case JsonTokenId.ID_NULL:
494+
// 20-Mar-2022, tatu: [databind#3421] Allow skipping `null`s from JSON
495+
if (!ctxt.isEnabled(JsonNodeFeature.READ_NULL_PROPERTIES)) {
496+
continue;
497+
}
488498
value = nodeFactory.nullNode();
489499
break;
490500
default:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.fasterxml.jackson.databind.node;
2+
3+
import com.fasterxml.jackson.databind.*;
4+
import com.fasterxml.jackson.databind.cfg.JsonNodeFeature;
5+
import com.fasterxml.jackson.databind.json.JsonMapper;
6+
7+
// Tests for new (2.14) `JsonNodeFeature`
8+
public class NodeFeaturesTest extends BaseMapTest
9+
{
10+
private final ObjectMapper MAPPER = newJsonMapper();
11+
private final ObjectReader READER = MAPPER.reader();
12+
13+
private final ObjectNode DOC_EMPTY = MAPPER.createObjectNode();
14+
private final ObjectNode DOC_WITH_NULL = MAPPER.createObjectNode();
15+
{
16+
DOC_WITH_NULL.putNull("nvl");
17+
}
18+
private final String JSON_WITH_NULL = a2q("{'nvl':null}");
19+
20+
public void testDefaultSettings() throws Exception
21+
{
22+
assertTrue(READER.isEnabled(JsonNodeFeature.READ_NULL_PROPERTIES));
23+
24+
assertFalse(READER.without(JsonNodeFeature.READ_NULL_PROPERTIES)
25+
.isEnabled(JsonNodeFeature.READ_NULL_PROPERTIES));
26+
}
27+
28+
public void testReadNulls() throws Exception
29+
{
30+
// so by default we'll get null included
31+
assertEquals(DOC_WITH_NULL, READER.readTree(JSON_WITH_NULL));
32+
33+
ObjectMapper noNullsMapper = JsonMapper.builder()
34+
.disable(JsonNodeFeature.READ_NULL_PROPERTIES)
35+
.build();
36+
ObjectReader r = noNullsMapper.reader();
37+
assertFalse(r.isEnabled(JsonNodeFeature.READ_NULL_PROPERTIES));
38+
assertEquals(DOC_EMPTY, r.readTree(JSON_WITH_NULL));
39+
40+
// but also verify we can "reset" reader's behavior
41+
ObjectReader r2 = r.with(JsonNodeFeature.READ_NULL_PROPERTIES);
42+
assertEquals(DOC_WITH_NULL, r2.readTree(JSON_WITH_NULL));
43+
44+
// and then bit more complex doc
45+
ObjectNode exp = noNullsMapper.createObjectNode();
46+
exp.put("a", 1);
47+
exp.put("c", true);
48+
assertEquals(exp, r.readTree(a2q("{'a':1,'b':null,'c':true}")));
49+
}
50+
}

0 commit comments

Comments
 (0)