Skip to content

Commit 6532c75

Browse files
committed
...
1 parent 21220ee commit 6532c75

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

+3-10
Original file line numberDiff line numberDiff line change
@@ -2393,9 +2393,7 @@ public <T> T readValue(JsonParser p, JavaType valueType)
23932393
public <T extends TreeNode> T readTree(JsonParser p)
23942394
throws IOException, JsonProcessingException
23952395
{
2396-
/* 05-Aug-2011, tatu: Also, must check for EOF here before
2397-
* calling readValue(), since that'll choke on it otherwise
2398-
*/
2396+
// Must check for EOF here before calling readValue(), since that'll choke on it otherwise
23992397
DeserializationConfig cfg = getDeserializationConfig();
24002398
JsonToken t = p.getCurrentToken();
24012399
if (t == null) {
@@ -2404,17 +2402,13 @@ public <T extends TreeNode> T readTree(JsonParser p)
24042402
return null;
24052403
}
24062404
}
2405+
// NOTE! _readValue() will check for trailing tokens
24072406
JsonNode n = (JsonNode) _readValue(cfg, p, JSON_NODE_TYPE);
24082407
if (n == null) {
24092408
n = getNodeFactory().nullNode();
24102409
}
24112410
@SuppressWarnings("unchecked")
24122411
T result = (T) n;
2413-
/*
2414-
if (cfg.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)) {
2415-
_verifyNoTrailingTokens(p, ctxt, valueType);
2416-
}
2417-
*/
24182412
return result;
24192413
}
24202414

@@ -4029,14 +4023,13 @@ protected JsonNode _readTreeAndClose(JsonParser p0) throws IOException
40294023
} else {
40304024
ctxt = createDeserializationContext(p, cfg);
40314025
JsonDeserializer<Object> deser = _findRootDeserializer(ctxt, valueType);
4032-
Object result;
40334026
if (cfg.useRootWrapping()) {
40344027
resultNode = (JsonNode) _unwrapAndDeserialize(p, ctxt, cfg, valueType, deser);
40354028
} else {
40364029
resultNode = (JsonNode) deser.deserialize(p, ctxt);
40374030
}
40384031
}
4039-
if (cfg.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)) {
4032+
if (checkTrailing) {
40404033
_verifyNoTrailingTokens(p, ctxt, valueType);
40414034
}
40424035
// No ObjectIds so can ignore

src/test/java/com/fasterxml/jackson/databind/node/EmptyContentAsTreeTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ public void testNullFromEOFWithParserAndReader() throws Exception
8989
_assertNullTree(MAPPER.reader().readTree(p));
9090
}
9191
}
92-
*/
93-
92+
*/
9493
// [databind#2211]: when passing content sources OTHER than `JsonParser`,
9594
// return "missing node" instead of alternate (return `null`, throw exception).
9695
public void testMissingNodeForEOFOtherMapper() throws Exception

src/test/java/com/fasterxml/jackson/databind/node/TreeReadViaMapperTest.java

+18-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
public class TreeReadViaMapperTest extends BaseMapTest
1616
{
17+
private final ObjectMapper MAPPER = objectMapper();
18+
1719
public void testSimple() throws Exception
1820
{
1921
final String JSON = SAMPLE_DOC_JSON_SPEC;
@@ -22,9 +24,9 @@ public void testSimple() throws Exception
2224
JsonNode result;
2325

2426
if (type == 0) {
25-
result = objectMapper().readTree(new StringReader(JSON));
27+
result = MAPPER.readTree(new StringReader(JSON));
2628
} else {
27-
result = objectMapper().readTree(JSON);
29+
result = MAPPER.readTree(JSON);
2830
}
2931

3032
assertType(result, ObjectNode.class);
@@ -90,9 +92,8 @@ public void testSimple() throws Exception
9092

9193
public void testMixed() throws IOException
9294
{
93-
ObjectMapper om = new ObjectMapper();
9495
String JSON = "{\"node\" : { \"a\" : 3 }, \"x\" : 9 }";
95-
Bean bean = om.readValue(JSON, Bean.class);
96+
Bean bean = MAPPER.readValue(JSON, Bean.class);
9697

9798
assertEquals(9, bean._x);
9899
JsonNode n = bean._node;
@@ -115,15 +116,26 @@ public void testEOF() throws Exception
115116
;
116117
JsonFactory jf = new JsonFactory();
117118
JsonParser p = jf.createParser(new StringReader(JSON));
118-
JsonNode result = objectMapper().readTree(p);
119+
JsonNode result = MAPPER.readTree(p);
119120

120121
assertTrue(result.isObject());
121122
assertEquals(4, result.size());
122123

123-
assertNull(objectMapper().readTree(p));
124+
assertNull(MAPPER.readTree(p));
124125
p.close();
125126
}
126127

128+
public void testNullViaParser() throws Exception
129+
{
130+
final String JSON = " null ";
131+
JsonFactory jf = new JsonFactory();
132+
133+
try (JsonParser p = jf.createParser(new StringReader(JSON))) {
134+
final JsonNode result = MAPPER.readTree(p);
135+
assertTrue(result.isNull());
136+
}
137+
}
138+
127139
public void testMultiple() throws Exception
128140
{
129141
String JSON = "12 \"string\" [ 1, 2, 3 ]";

0 commit comments

Comments
 (0)