@@ -2352,11 +2352,9 @@ public <T> MappingIterator<T> readValues(JsonParser p, TypeReference<?> valueTyp
2352
2352
* @throws JsonParseException if underlying input contains invalid content
2353
2353
* of type {@link JsonParser} supports (JSON for default case)
2354
2354
*/
2355
- public JsonNode readTree (InputStream in )
2356
- throws IOException , JsonProcessingException
2355
+ public JsonNode readTree (InputStream in ) throws IOException
2357
2356
{
2358
- JsonNode n = (JsonNode ) _readMapAndClose (_jsonFactory .createParser (in ), JSON_NODE_TYPE );
2359
- return (n == null ) ? NullNode .instance : n ;
2357
+ return _readTreeAndClose (_jsonFactory .createParser (in ));
2360
2358
}
2361
2359
2362
2360
/**
@@ -2382,11 +2380,8 @@ public JsonNode readTree(InputStream in)
2382
2380
* as a non-null {@link JsonNode} (one that returns <code>true</code>
2383
2381
* for {@link JsonNode#isNull()}
2384
2382
*/
2385
- public JsonNode readTree (Reader r )
2386
- throws IOException , JsonProcessingException
2387
- {
2388
- JsonNode n = (JsonNode ) _readMapAndClose (_jsonFactory .createParser (r ), JSON_NODE_TYPE );
2389
- return (n == null ) ? NullNode .instance : n ;
2383
+ public JsonNode readTree (Reader r ) throws IOException {
2384
+ return _readTreeAndClose (_jsonFactory .createParser (r ));
2390
2385
}
2391
2386
2392
2387
/**
@@ -2412,11 +2407,8 @@ public JsonNode readTree(Reader r)
2412
2407
* @throws JsonParseException if underlying input contains invalid content
2413
2408
* of type {@link JsonParser} supports (JSON for default case)
2414
2409
*/
2415
- public JsonNode readTree (String content )
2416
- throws IOException , JsonProcessingException
2417
- {
2418
- JsonNode n = (JsonNode ) _readMapAndClose (_jsonFactory .createParser (content ), JSON_NODE_TYPE );
2419
- return (n == null ) ? NullNode .instance : n ;
2410
+ public JsonNode readTree (String content ) throws IOException {
2411
+ return _readTreeAndClose (_jsonFactory .createParser (content ));
2420
2412
}
2421
2413
2422
2414
/**
@@ -2435,11 +2427,8 @@ public JsonNode readTree(String content)
2435
2427
* @throws JsonParseException if underlying input contains invalid content
2436
2428
* of type {@link JsonParser} supports (JSON for default case)
2437
2429
*/
2438
- public JsonNode readTree (byte [] content )
2439
- throws IOException , JsonProcessingException
2440
- {
2441
- JsonNode n = (JsonNode ) _readMapAndClose (_jsonFactory .createParser (content ), JSON_NODE_TYPE );
2442
- return (n == null ) ? NullNode .instance : n ;
2430
+ public JsonNode readTree (byte [] content ) throws IOException {
2431
+ return _readTreeAndClose (_jsonFactory .createParser (content ));
2443
2432
}
2444
2433
2445
2434
/**
@@ -2465,8 +2454,7 @@ public JsonNode readTree(byte[] content)
2465
2454
public JsonNode readTree (File file )
2466
2455
throws IOException , JsonProcessingException
2467
2456
{
2468
- JsonNode n = (JsonNode ) _readMapAndClose (_jsonFactory .createParser (file ), JSON_NODE_TYPE );
2469
- return (n == null ) ? NullNode .instance : n ;
2457
+ return _readTreeAndClose (_jsonFactory .createParser (file ));
2470
2458
}
2471
2459
2472
2460
/**
@@ -2489,11 +2477,8 @@ public JsonNode readTree(File file)
2489
2477
* @throws JsonParseException if underlying input contains invalid content
2490
2478
* of type {@link JsonParser} supports (JSON for default case)
2491
2479
*/
2492
- public JsonNode readTree (URL source )
2493
- throws IOException , JsonProcessingException
2494
- {
2495
- JsonNode n = (JsonNode ) _readMapAndClose (_jsonFactory .createParser (source ), JSON_NODE_TYPE );
2496
- return (n == null ) ? NullNode .instance : n ;
2480
+ public JsonNode readTree (URL source ) throws IOException {
2481
+ return _readTreeAndClose (_jsonFactory .createParser (source ));
2497
2482
}
2498
2483
2499
2484
/*
@@ -3837,12 +3822,50 @@ protected Object _readMapAndClose(JsonParser p0, JavaType valueType)
3837
3822
}
3838
3823
ctxt .checkUnresolvedObjectId ();
3839
3824
}
3840
- // Need to consume the token too
3841
- p .clearCurrentToken ();
3842
3825
return result ;
3843
3826
}
3844
3827
}
3845
3828
3829
+ /**
3830
+ * Similar to {@link #_readMapAndClose} but specialized for <code>JsonNode</code>
3831
+ * reading.
3832
+ *
3833
+ * @since 2.9
3834
+ */
3835
+ protected JsonNode _readTreeAndClose (JsonParser p0 ) throws IOException
3836
+ {
3837
+ try (JsonParser p = p0 ) {
3838
+ final JavaType valueType = JSON_NODE_TYPE ;
3839
+
3840
+ // 27-Oct-2016, tatu: Need to inline `_initForReading()` due to
3841
+ // special requirements by tree reading (no fail on eof)
3842
+
3843
+ _deserializationConfig .initialize (p ); // since 2.5
3844
+ JsonToken t = p .getCurrentToken ();
3845
+ if (t == null ) {
3846
+ t = p .nextToken ();
3847
+ if (t == null ) { // [databind#1406]: expose end-of-input as `null`
3848
+ return null ;
3849
+ }
3850
+ }
3851
+ if (t == JsonToken .VALUE_NULL ) {
3852
+ return _deserializationConfig .getNodeFactory ().nullNode ();
3853
+ }
3854
+ DeserializationConfig cfg = getDeserializationConfig ();
3855
+ DeserializationContext ctxt = createDeserializationContext (p , cfg );
3856
+ JsonDeserializer <Object > deser = _findRootDeserializer (ctxt , valueType );
3857
+ Object result ;
3858
+ if (cfg .useRootWrapping ()) {
3859
+ result = _unwrapAndDeserialize (p , ctxt , cfg , valueType , deser );
3860
+ } else {
3861
+ result = deser .deserialize (p , ctxt );
3862
+ }
3863
+ // No ObjectIds so can ignore
3864
+ // ctxt.checkUnresolvedObjectId();
3865
+ return (JsonNode ) result ;
3866
+ }
3867
+ }
3868
+
3846
3869
/**
3847
3870
* Method called to ensure that given parser is ready for reading
3848
3871
* content for data binding.
0 commit comments