Skip to content

Commit

Permalink
More work on #2211
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 23, 2019
1 parent aea65f1 commit f0abe41
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 43 deletions.
46 changes: 29 additions & 17 deletions src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2390,10 +2390,6 @@ public <T> T readValue(JsonParser p, JavaType valueType)
public <T extends TreeNode> T readTree(JsonParser p)
throws IOException, JsonProcessingException
{
/* 02-Mar-2009, tatu: One twist; deserialization provider
* will map JSON null straight into Java null. But what
* we want to return is the "null node" instead.
*/
/* 05-Aug-2011, tatu: Also, must check for EOF here before
* calling readValue(), since that'll choke on it otherwise
*/
Expand All @@ -2411,6 +2407,11 @@ public <T extends TreeNode> T readTree(JsonParser p)
}
@SuppressWarnings("unchecked")
T result = (T) n;
/*
if (cfg.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)) {
_verifyNoTrailingTokens(p, ctxt, valueType);
}
*/
return result;
}

Expand Down Expand Up @@ -4087,27 +4088,38 @@ protected JsonNode _readTreeAndClose(JsonParser p0) throws IOException
JsonToken t = p.getCurrentToken();
if (t == null) {
t = p.nextToken();
if (t == null) { // [databind#1406]: expose end-of-input as `null`
return null;
if (t == null) {
// [databind#2211]: return `MissingNode` (supercedes [databind#1406] which dictated
// returning `null`
return cfg.getNodeFactory().missingNode();
}
}
final boolean checkTrailing = cfg.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
DeserializationContext ctxt;
JsonNode resultNode;

if (t == JsonToken.VALUE_NULL) {
return cfg.getNodeFactory().nullNode();
}
DeserializationContext ctxt = createDeserializationContext(p, cfg);
JsonDeserializer<Object> deser = _findRootDeserializer(ctxt, valueType);
Object result;
if (cfg.useRootWrapping()) {
result = _unwrapAndDeserialize(p, ctxt, cfg, valueType, deser);
resultNode = cfg.getNodeFactory().nullNode();
if (!checkTrailing) {
return resultNode;
}
ctxt = createDeserializationContext(p, cfg);
} else {
result = deser.deserialize(p, ctxt);
if (cfg.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)) {
_verifyNoTrailingTokens(p, ctxt, valueType);
ctxt = createDeserializationContext(p, cfg);
JsonDeserializer<Object> deser = _findRootDeserializer(ctxt, valueType);
Object result;
if (cfg.useRootWrapping()) {
resultNode = (JsonNode) _unwrapAndDeserialize(p, ctxt, cfg, valueType, deser);
} else {
resultNode = (JsonNode) deser.deserialize(p, ctxt);
}
}
if (cfg.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)) {
_verifyNoTrailingTokens(p, ctxt, valueType);
}
// No ObjectIds so can ignore
// ctxt.checkUnresolvedObjectId();
return (JsonNode) result;
return resultNode;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1670,12 +1670,14 @@ protected final JsonNode _bindAsTree(JsonParser p) throws IOException
if (t == null) {
t = p.nextToken();
if (t == null) { // [databind#1406]: expose end-of-input as `null`
return null;
// [databind#2211]: return `MissingNode` (supercedes [databind#1406] which dictated
// returning `null`
return _config.getNodeFactory().missingNode();
}
}
DeserializationContext ctxt = createDeserializationContext(p);
if (t == JsonToken.VALUE_NULL) {
return ctxt.getNodeFactory().nullNode();
return _config.getNodeFactory().nullNode();
}
JsonDeserializer<Object> deser = _findTreeDeserializer(ctxt);
Object result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void testNullFromEOFWithParserAndMapper() throws Exception
}

// [databind#1406]
/*
public void testNullFromEOFWithParserAndReader() throws Exception
{
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY0)) {
Expand Down Expand Up @@ -88,6 +89,7 @@ public void testNullFromEOFWithParserAndReader() throws Exception
_assertNullTree(MAPPER.reader().readTree(p));
}
}
*/

// [databind#2211]: when passing content sources OTHER than `JsonParser`,
// return "missing node" instead of alternate (return `null`, throw exception).
Expand Down Expand Up @@ -131,11 +133,9 @@ private void _assertNullTree(TreeNode n) {
}

private void _assertMissing(JsonNode n) {
/*
assertNotNull("Should not get `null` but `MissingNode`", n);
if (!n.isMissingNode()) {
fail("Should get `MissingNode` but got: "+n.getClass().getName());
}
*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,28 +151,6 @@ public void testMultiple() throws Exception
p.close();
}

// [databind#1406]
public void testNullFromEOFViaMapper() throws Exception
{
final ObjectMapper mapper = objectMapper();

assertNull(mapper.readTree(new StringReader("")));
assertNull(mapper.readTree(new ByteArrayInputStream(new byte[0])));
}

// [databind#1406]
public void testNullFromEOFViaObjectReader() throws Exception
{
final ObjectMapper mapper = objectMapper();

assertNull(mapper.readTree(new StringReader("")));
assertNull(mapper.readTree(new ByteArrayInputStream(new byte[0])));
assertNull(mapper.readerFor(JsonNode.class)
.readTree(new StringReader("")));
assertNull(mapper.readerFor(JsonNode.class)
.readTree(new ByteArrayInputStream(new byte[0])));
}

/*
/**********************************************
/* Helper methods
Expand Down

0 comments on commit f0abe41

Please sign in to comment.