Skip to content

Commit 0404c48

Browse files
committed
Fix #2647 (last part)
1 parent ce04976 commit 0404c48

14 files changed

+184
-44
lines changed

release-notes/VERSION-2.x

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Project: jackson-databind
4444
(reported by Oleksii K)
4545
#2643: Change default textual serialization of `java.util.Date`/`Calendar`
4646
to include colon in timezone offset
47+
#2647: Add `ObjectMapper.createParser()` and `createGenerator()` methods
4748
- Add `SerializerProvider.findContentValueSerializer()` methods
4849

4950
2.10.4 (not yet released)

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

+142-2
Original file line numberDiff line numberDiff line change
@@ -1175,8 +1175,148 @@ public JsonGenerator createGenerator(DataOutput out) throws IOException {
11751175
/**********************************************************
11761176
*/
11771177

1178-
// TODO
1179-
1178+
/**
1179+
* Factory method for constructing properly initialized {@link JsonParser}
1180+
* to read content from specified {@link File}.
1181+
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
1182+
* for properly closing it once content reading is complete.
1183+
*
1184+
* @since 2.11
1185+
*/
1186+
public JsonParser createParser(File src) throws IOException {
1187+
_assertNotNull("src", src);
1188+
return _jsonFactory.createParser(src);
1189+
}
1190+
1191+
/**
1192+
* Factory method for constructing properly initialized {@link JsonParser}
1193+
* to read content from specified {@link File}.
1194+
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
1195+
* for properly closing it once content reading is complete.
1196+
*
1197+
* @since 2.11
1198+
*/
1199+
public JsonParser createParser(URL src) throws IOException {
1200+
_assertNotNull("src", src);
1201+
return _jsonFactory.createParser(src);
1202+
}
1203+
1204+
/**
1205+
* Factory method for constructing properly initialized {@link JsonParser}
1206+
* to read content using specified {@link InputStream}.
1207+
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
1208+
* for properly closing it once content reading is complete.
1209+
*
1210+
* @since 2.11
1211+
*/
1212+
public JsonParser createParser(InputStream in) throws IOException {
1213+
_assertNotNull("in", in);
1214+
return _jsonFactory.createParser(in);
1215+
}
1216+
1217+
/**
1218+
* Factory method for constructing properly initialized {@link JsonParser}
1219+
* to read content using specified {@link Reader}.
1220+
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
1221+
* for properly closing it once content reading is complete.
1222+
*
1223+
* @since 2.11
1224+
*/
1225+
public JsonParser createParser(Reader r) throws IOException {
1226+
_assertNotNull("r", r);
1227+
return _jsonFactory.createParser(r);
1228+
}
1229+
1230+
/**
1231+
* Factory method for constructing properly initialized {@link JsonParser}
1232+
* to read content from specified byte array.
1233+
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
1234+
* for properly closing it once content reading is complete.
1235+
*
1236+
* @since 2.11
1237+
*/
1238+
public JsonParser createParser(byte[] content) throws IOException {
1239+
_assertNotNull("content", content);
1240+
return _jsonFactory.createParser(content);
1241+
}
1242+
1243+
/**
1244+
* Factory method for constructing properly initialized {@link JsonParser}
1245+
* to read content from specified byte array.
1246+
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
1247+
* for properly closing it once content reading is complete.
1248+
*
1249+
* @since 2.11
1250+
*/
1251+
public JsonParser createParser(byte[] content, int offset, int len) throws IOException {
1252+
_assertNotNull("content", content);
1253+
return _jsonFactory.createParser(content, offset, len);
1254+
}
1255+
1256+
/**
1257+
* Factory method for constructing properly initialized {@link JsonParser}
1258+
* to read content from specified String.
1259+
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
1260+
* for properly closing it once content reading is complete.
1261+
*
1262+
* @since 2.11
1263+
*/
1264+
public JsonParser createParser(String content) throws IOException {
1265+
_assertNotNull("content", content);
1266+
return _jsonFactory.createParser(content);
1267+
}
1268+
1269+
/**
1270+
* Factory method for constructing properly initialized {@link JsonParser}
1271+
* to read content from specified character array
1272+
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
1273+
* for properly closing it once content reading is complete.
1274+
*
1275+
* @since 2.11
1276+
*/
1277+
public JsonParser createParser(char[] content) throws IOException {
1278+
_assertNotNull("content", content);
1279+
return _jsonFactory.createParser(content);
1280+
}
1281+
1282+
/**
1283+
* Factory method for constructing properly initialized {@link JsonParser}
1284+
* to read content from specified character array.
1285+
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
1286+
* for properly closing it once content reading is complete.
1287+
*
1288+
* @since 2.11
1289+
*/
1290+
public JsonParser createParser(char[] content, int offset, int len) throws IOException {
1291+
_assertNotNull("content", content);
1292+
return _jsonFactory.createParser(content, offset, len);
1293+
}
1294+
1295+
/**
1296+
* Factory method for constructing properly initialized {@link JsonParser}
1297+
* to read content using specified {@link DataInput}.
1298+
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
1299+
* for properly closing it once content reading is complete.
1300+
*
1301+
* @since 2.11
1302+
*/
1303+
public JsonParser createParser(DataInput content) throws IOException {
1304+
_assertNotNull("content", content);
1305+
return _jsonFactory.createParser(content);
1306+
}
1307+
1308+
/**
1309+
* Factory method for constructing properly initialized {@link JsonParser}
1310+
* to read content using non-blocking (asynchronous) mode.
1311+
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
1312+
* for properly closing it once content reading is complete.
1313+
*
1314+
* @since 2.11
1315+
*/
1316+
public JsonParser createNonBlockingByteArrayParser() throws IOException {
1317+
return _jsonFactory.createNonBlockingByteArrayParser();
1318+
}
1319+
11801320
/*
11811321
/**********************************************************
11821322
/* Configuration: main config object access

src/test/java/com/fasterxml/jackson/databind/ObjectReaderTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static class POJO {
3030
public void testSimpleViaParser() throws Exception
3131
{
3232
final String JSON = "[1]";
33-
JsonParser p = MAPPER.getFactory().createParser(JSON);
33+
JsonParser p = MAPPER.createParser(JSON);
3434
Object ob = MAPPER.readerFor(Object.class)
3535
.readValue(p);
3636
p.close();

src/test/java/com/fasterxml/jackson/databind/deser/TestJacksonTypes.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void testTokenBufferWithSequence() throws Exception
113113
// [databind#2398]
114114
public void testDeeplyNestedArrays() throws Exception
115115
{
116-
try (JsonParser p = MAPPER.tokenStreamFactory().createParser(_createNested(RECURSION_2398 * 2,
116+
try (JsonParser p = MAPPER.createParser(_createNested(RECURSION_2398 * 2,
117117
"[", " 123 ", "]"))) {
118118
p.nextToken();
119119
TokenBuffer b = new TokenBuffer(p);
@@ -124,7 +124,7 @@ public void testDeeplyNestedArrays() throws Exception
124124

125125
public void testDeeplyNestedObjects() throws Exception
126126
{
127-
try (JsonParser p = MAPPER.tokenStreamFactory().createParser(_createNested(RECURSION_2398,
127+
try (JsonParser p = MAPPER.createParser(_createNested(RECURSION_2398,
128128
"{\"a\":", "42", "}"))) {
129129
p.nextToken();
130130
TokenBuffer b = new TokenBuffer(p);

src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumDeserializationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public void testSimple() throws Exception
228228
// First "good" case with Strings
229229
String JSON = "\"OK\" \"RULES\" null";
230230
// multiple main-level mappings, need explicit parser:
231-
JsonParser jp = MAPPER.getFactory().createParser(JSON);
231+
JsonParser jp = MAPPER.createParser(JSON);
232232

233233
assertEquals(TestEnum.OK, MAPPER.readValue(jp, TestEnum.class));
234234
assertEquals(TestEnum.RULES, MAPPER.readValue(jp, TestEnum.class));

src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKScalarsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ public void testSequenceOfInts() throws Exception
606606
sb.append(" ");
607607
sb.append(i);
608608
}
609-
JsonParser jp = MAPPER.getFactory().createParser(sb.toString());
609+
JsonParser jp = MAPPER.createParser(sb.toString());
610610
for (int i = 0; i < NR_OF_INTS; ++i) {
611611
Integer result = MAPPER.readValue(jp, Integer.class);
612612
assertEquals(Integer.valueOf(i), result);

src/test/java/com/fasterxml/jackson/databind/exc/DeserExceptionTypeTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void testExceptionWithIncomplete()
7575
throws Exception
7676
{
7777
BrokenStringReader r = new BrokenStringReader("[ 1, ", "TEST");
78-
JsonParser p = MAPPER.getFactory().createParser(r);
78+
JsonParser p = MAPPER.createParser(r);
7979
try {
8080
@SuppressWarnings("unused")
8181
Object ob = MAPPER.readValue(p, Object.class);
@@ -90,7 +90,7 @@ public void testExceptionWithIncomplete()
9090

9191
public void testExceptionWithEOF() throws Exception
9292
{
93-
JsonParser p = MAPPER.getFactory().createParser(" 3");
93+
JsonParser p = MAPPER.createParser(" 3");
9494

9595
Integer I = MAPPER.readValue(p, Integer.class);
9696
assertEquals(3, I.intValue());

src/test/java/com/fasterxml/jackson/databind/exc/ExceptionSerializationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void testSimple() throws Exception
7070
// to double-check [databind#1413]
7171
public void testSimpleOther() throws Exception
7272
{
73-
JsonParser p = MAPPER.getFactory().createParser("{ }");
73+
JsonParser p = MAPPER.createParser("{ }");
7474
InvalidFormatException exc = InvalidFormatException.from(p, "Test", getClass(), String.class);
7575
String json = MAPPER.writeValueAsString(exc);
7676
p.close();

src/test/java/com/fasterxml/jackson/databind/misc/ParsingContext2525Test.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ public class ParsingContext2525Test extends BaseMapTest
2828

2929
public void testAllWithRegularParser() throws Exception
3030
{
31-
try (JsonParser p = MAPPER.getFactory().createParser(MINIMAL_ARRAY_DOC)) {
31+
try (JsonParser p = MAPPER.createParser(MINIMAL_ARRAY_DOC)) {
3232
_testSimpleArrayUsingPathAsPointer(p);
3333
}
34-
try (JsonParser p = MAPPER.getFactory().createParser(MINIMAL_OBJECT_DOC)) {
34+
try (JsonParser p = MAPPER.createParser(MINIMAL_OBJECT_DOC)) {
3535
_testSimpleObjectUsingPathAsPointer(p);
3636
}
37-
try (JsonParser p = MAPPER.getFactory().createParser(FULL_DOC)) {
37+
try (JsonParser p = MAPPER.createParser(FULL_DOC)) {
3838
_testFullDocUsingPathAsPointer(p);
3939
}
4040
}
@@ -68,7 +68,7 @@ public void testFullDocWithBuffer() throws Exception
6868

6969
private TokenBuffer _readAsTokenBuffer(String doc) throws IOException
7070
{
71-
try (JsonParser p = MAPPER.getFactory().createParser(doc)) {
71+
try (JsonParser p = MAPPER.createParser(doc)) {
7272
p.nextToken();
7373
return TokenBuffer.asCopyOfValue(p)
7474
.overrideParentContext(null);

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

+18-18
Original file line numberDiff line numberDiff line change
@@ -25,66 +25,66 @@ public class EmptyContentAsTreeTest extends BaseMapTest
2525

2626
public void testNullFromEOFWithParserAndMapper() throws Exception
2727
{
28-
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY0)) {
28+
try (JsonParser p = MAPPER.createParser(EMPTY0)) {
2929
_assertNullTree(MAPPER.readTree(p));
3030
}
31-
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1)) {
31+
try (JsonParser p = MAPPER.createParser(EMPTY1)) {
3232
_assertNullTree(MAPPER.readTree(p));
3333
}
34-
try (JsonParser p = MAPPER.getFactory().createParser(new StringReader(EMPTY0))) {
34+
try (JsonParser p = MAPPER.createParser(new StringReader(EMPTY0))) {
3535
_assertNullTree(MAPPER.readTree(p));
3636
}
37-
try (JsonParser p = MAPPER.getFactory().createParser(new StringReader(EMPTY1))) {
37+
try (JsonParser p = MAPPER.createParser(new StringReader(EMPTY1))) {
3838
_assertNullTree(MAPPER.readTree(p));
3939
}
4040

41-
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY0_BYTES)) {
41+
try (JsonParser p = MAPPER.createParser(EMPTY0_BYTES)) {
4242
_assertNullTree(MAPPER.readTree(p));
4343
}
44-
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1_BYTES)) {
44+
try (JsonParser p = MAPPER.createParser(EMPTY1_BYTES)) {
4545
_assertNullTree(MAPPER.readTree(p));
4646
}
47-
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1_BYTES, 0, EMPTY1_BYTES.length)) {
47+
try (JsonParser p = MAPPER.createParser(EMPTY1_BYTES, 0, EMPTY1_BYTES.length)) {
4848
_assertNullTree(MAPPER.readTree(p));
4949
}
50-
try (JsonParser p = MAPPER.getFactory().createParser(new ByteArrayInputStream(EMPTY0_BYTES))) {
50+
try (JsonParser p = MAPPER.createParser(new ByteArrayInputStream(EMPTY0_BYTES))) {
5151
_assertNullTree(MAPPER.readTree(p));
5252
}
53-
try (JsonParser p = MAPPER.getFactory().createParser(new ByteArrayInputStream(EMPTY1_BYTES))) {
53+
try (JsonParser p = MAPPER.createParser(new ByteArrayInputStream(EMPTY1_BYTES))) {
5454
_assertNullTree(MAPPER.readTree(p));
5555
}
5656
}
5757

5858
// [databind#1406]
5959
public void testNullFromEOFWithParserAndReader() throws Exception
6060
{
61-
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY0)) {
61+
try (JsonParser p = MAPPER.createParser(EMPTY0)) {
6262
_assertNullTree(MAPPER.reader().readTree(p));
6363
}
64-
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1)) {
64+
try (JsonParser p = MAPPER.createParser(EMPTY1)) {
6565
_assertNullTree(MAPPER.reader().readTree(p));
6666
}
67-
try (JsonParser p = MAPPER.getFactory().createParser(new StringReader(EMPTY0))) {
67+
try (JsonParser p = MAPPER.createParser(new StringReader(EMPTY0))) {
6868
_assertNullTree(MAPPER.reader().readTree(p));
6969
}
70-
try (JsonParser p = MAPPER.getFactory().createParser(new StringReader(EMPTY1))) {
70+
try (JsonParser p = MAPPER.createParser(new StringReader(EMPTY1))) {
7171
_assertNullTree(MAPPER.reader().readTree(p));
7272
}
7373

74-
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY0_BYTES)) {
74+
try (JsonParser p = MAPPER.createParser(EMPTY0_BYTES)) {
7575
_assertNullTree(MAPPER.reader().readTree(p));
7676
}
77-
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1_BYTES)) {
77+
try (JsonParser p = MAPPER.createParser(EMPTY1_BYTES)) {
7878
_assertNullTree(MAPPER.reader().readTree(p));
7979
}
80-
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1_BYTES, 0, EMPTY1_BYTES.length)) {
80+
try (JsonParser p = MAPPER.createParser(EMPTY1_BYTES, 0, EMPTY1_BYTES.length)) {
8181
_assertNullTree(MAPPER.reader().readTree(p));
8282
}
8383

84-
try (JsonParser p = MAPPER.getFactory().createParser(new ByteArrayInputStream(EMPTY0_BYTES))) {
84+
try (JsonParser p = MAPPER.createParser(new ByteArrayInputStream(EMPTY0_BYTES))) {
8585
_assertNullTree(MAPPER.reader().readTree(p));
8686
}
87-
try (JsonParser p = MAPPER.getFactory().createParser(new ByteArrayInputStream(EMPTY1_BYTES))) {
87+
try (JsonParser p = MAPPER.createParser(new ByteArrayInputStream(EMPTY1_BYTES))) {
8888
_assertNullTree(MAPPER.reader().readTree(p));
8989
}
9090
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class TreeFromIncompleteJsonTest extends BaseMapTest
1313
public void testErrorHandling() throws IOException {
1414

1515
String json = "{\"A\":{\"B\":\n";
16-
JsonParser parser = MAPPER.getFactory().createParser(json);
16+
JsonParser parser = MAPPER.createParser(json);
1717
try {
1818
parser.readValueAsTree();
1919
} catch (JsonEOFException e) {

0 commit comments

Comments
 (0)