Skip to content

Commit aea65f1

Browse files
committed
Working on tests for #2211
1 parent 9659d43 commit aea65f1

File tree

3 files changed

+143
-13
lines changed

3 files changed

+143
-13
lines changed

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -2594,7 +2594,14 @@ public JsonNode readTree(String content) throws IOException {
25942594
public JsonNode readTree(byte[] content) throws IOException {
25952595
return _readTreeAndClose(_jsonFactory.createParser(content));
25962596
}
2597-
2597+
2598+
/**
2599+
* @since 2.10
2600+
*/
2601+
public JsonNode readTree(byte[] content, int offset, int len) throws IOException {
2602+
return _readTreeAndClose(_jsonFactory.createParser(content, offset, len));
2603+
}
2604+
25982605
/**
25992606
* Method to deserialize JSON content as tree expressed using set of {@link JsonNode} instances.
26002607
* Returns root of the resulting tree (where root can consist of just a single node if the current

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

+28
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,12 @@ public <T> T readValue(DataInput src) throws IOException
13071307
return (T) _bindAndClose(_considerFilter(_parserFactory.createParser(src), false));
13081308
}
13091309

1310+
/*
1311+
/**********************************************************
1312+
/* Deserialization methods; JsonNode ("tree")
1313+
/**********************************************************
1314+
*/
1315+
13101316
/**
13111317
* Method that reads content from given input source,
13121318
* using configuration of this reader, and binds it as JSON Tree.
@@ -1358,6 +1364,28 @@ public JsonNode readTree(String json) throws IOException
13581364
return _bindAndCloseAsTree(_considerFilter(_parserFactory.createParser(json), false));
13591365
}
13601366

1367+
/**
1368+
* @since 2.10
1369+
*/
1370+
public JsonNode readTree(byte[] json) throws IOException
1371+
{
1372+
if (_dataFormatReaders != null) {
1373+
_reportUndetectableSource(json);
1374+
}
1375+
return _bindAndCloseAsTree(_considerFilter(_parserFactory.createParser(json), false));
1376+
}
1377+
1378+
/**
1379+
* @since 2.10
1380+
*/
1381+
public JsonNode readTree(byte[] json, int offset, int len) throws IOException
1382+
{
1383+
if (_dataFormatReaders != null) {
1384+
_reportUndetectableSource(json);
1385+
}
1386+
return _bindAndCloseAsTree(_considerFilter(_parserFactory.createParser(json, offset, len), false));
1387+
}
1388+
13611389
public JsonNode readTree(DataInput src) throws IOException
13621390
{
13631391
if (_dataFormatReaders != null) {

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

+107-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import java.io.ByteArrayInputStream;
44
import java.io.StringReader;
5+
import java.nio.charset.StandardCharsets;
56

7+
import com.fasterxml.jackson.core.JsonParser;
8+
import com.fasterxml.jackson.core.TreeNode;
69
import com.fasterxml.jackson.databind.*;
710

811
/**
@@ -12,35 +15,127 @@ public class EmptyContentAsTreeTest extends BaseMapTest
1215
{
1316
private final ObjectMapper MAPPER = objectMapper();
1417

18+
private final String EMPTY0 = "";
19+
private final byte[] EMPTY0_BYTES = EMPTY0.getBytes(StandardCharsets.UTF_8);
20+
private final String EMPTY1 = " \n\t ";
21+
private final byte[] EMPTY1_BYTES = EMPTY1.getBytes(StandardCharsets.UTF_8);
22+
1523
// [databind#1406]: when passing `JsonParser`, indicate lack of content
1624
// by returning `null`
1725

18-
public void testNullFromEOFWithParser() throws Exception
26+
public void testNullFromEOFWithParserAndMapper() throws Exception
1927
{
20-
assertNull(MAPPER.readTree(new StringReader("")));
21-
assertNull(MAPPER.readTree(new ByteArrayInputStream(new byte[0])));
28+
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY0)) {
29+
_assertNullTree(MAPPER.readTree(p));
30+
}
31+
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1)) {
32+
_assertNullTree(MAPPER.readTree(p));
33+
}
34+
try (JsonParser p = MAPPER.getFactory().createParser(new StringReader(EMPTY0))) {
35+
_assertNullTree(MAPPER.readTree(p));
36+
}
37+
try (JsonParser p = MAPPER.getFactory().createParser(new StringReader(EMPTY1))) {
38+
_assertNullTree(MAPPER.readTree(p));
39+
}
40+
41+
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY0_BYTES)) {
42+
_assertNullTree(MAPPER.readTree(p));
43+
}
44+
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1_BYTES)) {
45+
_assertNullTree(MAPPER.readTree(p));
46+
}
47+
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1_BYTES, 0, EMPTY1_BYTES.length)) {
48+
_assertNullTree(MAPPER.readTree(p));
49+
}
50+
try (JsonParser p = MAPPER.getFactory().createParser(new ByteArrayInputStream(EMPTY0_BYTES))) {
51+
_assertNullTree(MAPPER.readTree(p));
52+
}
53+
try (JsonParser p = MAPPER.getFactory().createParser(new ByteArrayInputStream(EMPTY1_BYTES))) {
54+
_assertNullTree(MAPPER.readTree(p));
55+
}
2256
}
2357

2458
// [databind#1406]
25-
public void testNullFromEOFWithParserViaReader() throws Exception
59+
public void testNullFromEOFWithParserAndReader() throws Exception
2660
{
27-
assertNull(MAPPER.readTree(new StringReader("")));
28-
assertNull(MAPPER.readTree(new ByteArrayInputStream(new byte[0])));
29-
assertNull(MAPPER.readerFor(JsonNode.class)
30-
.readTree(new StringReader("")));
31-
assertNull(MAPPER.readerFor(JsonNode.class)
32-
.readTree(new ByteArrayInputStream(new byte[0])));
61+
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY0)) {
62+
_assertNullTree(MAPPER.reader().readTree(p));
63+
}
64+
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1)) {
65+
_assertNullTree(MAPPER.reader().readTree(p));
66+
}
67+
try (JsonParser p = MAPPER.getFactory().createParser(new StringReader(EMPTY0))) {
68+
_assertNullTree(MAPPER.reader().readTree(p));
69+
}
70+
try (JsonParser p = MAPPER.getFactory().createParser(new StringReader(EMPTY1))) {
71+
_assertNullTree(MAPPER.reader().readTree(p));
72+
}
73+
74+
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY0_BYTES)) {
75+
_assertNullTree(MAPPER.reader().readTree(p));
76+
}
77+
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1_BYTES)) {
78+
_assertNullTree(MAPPER.reader().readTree(p));
79+
}
80+
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1_BYTES, 0, EMPTY1_BYTES.length)) {
81+
_assertNullTree(MAPPER.reader().readTree(p));
82+
}
83+
84+
try (JsonParser p = MAPPER.getFactory().createParser(new ByteArrayInputStream(EMPTY0_BYTES))) {
85+
_assertNullTree(MAPPER.reader().readTree(p));
86+
}
87+
try (JsonParser p = MAPPER.getFactory().createParser(new ByteArrayInputStream(EMPTY1_BYTES))) {
88+
_assertNullTree(MAPPER.reader().readTree(p));
89+
}
3390
}
3491

3592
// [databind#2211]: when passing content sources OTHER than `JsonParser`,
3693
// return "missing node" instead of alternate (return `null`, throw exception).
37-
public void testMissingNodeForEOFOther() throws Exception
94+
public void testMissingNodeForEOFOtherMapper() throws Exception
3895
{
96+
_assertMissing(MAPPER.readTree(EMPTY0));
97+
_assertMissing(MAPPER.readTree(EMPTY1));
98+
_assertMissing(MAPPER.readTree(new StringReader(EMPTY0)));
99+
_assertMissing(MAPPER.readTree(new StringReader(EMPTY1)));
39100

101+
_assertMissing(MAPPER.readTree(EMPTY0_BYTES));
102+
_assertMissing(MAPPER.readTree(EMPTY0_BYTES, 0, EMPTY0_BYTES.length));
103+
_assertMissing(MAPPER.readTree(new ByteArrayInputStream(EMPTY0_BYTES)));
104+
_assertMissing(MAPPER.readTree(EMPTY1_BYTES));
105+
_assertMissing(MAPPER.readTree(EMPTY1_BYTES, 0, EMPTY1_BYTES.length));
106+
_assertMissing(MAPPER.readTree(new ByteArrayInputStream(EMPTY1_BYTES)));
107+
108+
// Assume File, URL, etc are fine. Note: `DataInput` probably can't be made to
109+
// work since it can not easily/gracefully handle unexpected end-of-input
40110
}
41111

42-
public void testMissingNodeForEOFOtherViaReader() throws Exception
112+
public void testMissingNodeViaObjectReader() throws Exception
43113
{
114+
_assertMissing(MAPPER.reader().readTree(EMPTY0));
115+
_assertMissing(MAPPER.reader().readTree(EMPTY1));
116+
_assertMissing(MAPPER.reader().readTree(new StringReader(EMPTY0)));
117+
_assertMissing(MAPPER.reader().readTree(new StringReader(EMPTY1)));
44118

119+
_assertMissing(MAPPER.reader().readTree(EMPTY0_BYTES));
120+
_assertMissing(MAPPER.reader().readTree(EMPTY0_BYTES, 0, EMPTY0_BYTES.length));
121+
_assertMissing(MAPPER.reader().readTree(new ByteArrayInputStream(EMPTY0_BYTES)));
122+
_assertMissing(MAPPER.reader().readTree(EMPTY1_BYTES));
123+
_assertMissing(MAPPER.reader().readTree(EMPTY1_BYTES, 0, EMPTY1_BYTES.length));
124+
_assertMissing(MAPPER.reader().readTree(new ByteArrayInputStream(EMPTY1_BYTES)));
125+
}
126+
127+
private void _assertNullTree(TreeNode n) {
128+
if (n != null) {
129+
fail("Should get `null` for reads with `JsonParser`, instead got: "+n.getClass().getName());
130+
}
131+
}
132+
133+
private void _assertMissing(JsonNode n) {
134+
/*
135+
assertNotNull("Should not get `null` but `MissingNode`", n);
136+
if (!n.isMissingNode()) {
137+
fail("Should get `MissingNode` but got: "+n.getClass().getName());
138+
}
139+
*/
45140
}
46141
}

0 commit comments

Comments
 (0)