Skip to content

Commit

Permalink
Fix #3421 (add JsonNodeFeature.READ_NULL_PROPERTIES)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 20, 2022
1 parent a8a233f commit 8bf0c05
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Project: jackson-databind

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

2.13.3 (not yet released)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.util.Arrays;

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.JsonNodeFeature;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.node.*;
import com.fasterxml.jackson.databind.type.LogicalType;
Expand Down Expand Up @@ -402,6 +404,10 @@ protected final JsonNode updateObject(JsonParser p, DeserializationContext ctxt,
value = nodeFactory.booleanNode(false);
break;
case JsonTokenId.ID_NULL:
// 20-Mar-2022, tatu: [databind#3421] Allow skipping `null`s from JSON
if (!ctxt.isEnabled(JsonNodeFeature.READ_NULL_PROPERTIES)) {
continue;
}
value = nodeFactory.nullNode();
break;
default:
Expand Down Expand Up @@ -485,6 +491,10 @@ protected final ContainerNode<?> _deserializeContainerNoRecursion(JsonParser p,
value = nodeFactory.booleanNode(false);
break;
case JsonTokenId.ID_NULL:
// 20-Mar-2022, tatu: [databind#3421] Allow skipping `null`s from JSON
if (!ctxt.isEnabled(JsonNodeFeature.READ_NULL_PROPERTIES)) {
continue;
}
value = nodeFactory.nullNode();
break;
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.fasterxml.jackson.databind.node;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.JsonNodeFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;

// Tests for new (2.14) `JsonNodeFeature`
public class NodeFeaturesTest extends BaseMapTest
{
private final ObjectMapper MAPPER = newJsonMapper();
private final ObjectReader READER = MAPPER.reader();

private final ObjectNode DOC_EMPTY = MAPPER.createObjectNode();
private final ObjectNode DOC_WITH_NULL = MAPPER.createObjectNode();
{
DOC_WITH_NULL.putNull("nvl");
}
private final String JSON_WITH_NULL = a2q("{'nvl':null}");

public void testDefaultSettings() throws Exception
{
assertTrue(READER.isEnabled(JsonNodeFeature.READ_NULL_PROPERTIES));

assertFalse(READER.without(JsonNodeFeature.READ_NULL_PROPERTIES)
.isEnabled(JsonNodeFeature.READ_NULL_PROPERTIES));
}

public void testReadNulls() throws Exception
{
// so by default we'll get null included
assertEquals(DOC_WITH_NULL, READER.readTree(JSON_WITH_NULL));

ObjectMapper noNullsMapper = JsonMapper.builder()
.disable(JsonNodeFeature.READ_NULL_PROPERTIES)
.build();
ObjectReader r = noNullsMapper.reader();
assertFalse(r.isEnabled(JsonNodeFeature.READ_NULL_PROPERTIES));
assertEquals(DOC_EMPTY, r.readTree(JSON_WITH_NULL));

// but also verify we can "reset" reader's behavior
ObjectReader r2 = r.with(JsonNodeFeature.READ_NULL_PROPERTIES);
assertEquals(DOC_WITH_NULL, r2.readTree(JSON_WITH_NULL));

// and then bit more complex doc
ObjectNode exp = noNullsMapper.createObjectNode();
exp.put("a", 1);
exp.put("c", true);
assertEquals(exp, r.readTree(a2q("{'a':1,'b':null,'c':true}")));
}
}

0 comments on commit 8bf0c05

Please sign in to comment.