Skip to content

Commit

Permalink
Fix #2647 (last part)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 8, 2020
1 parent ce04976 commit 0404c48
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 44 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Project: jackson-databind
(reported by Oleksii K)
#2643: Change default textual serialization of `java.util.Date`/`Calendar`
to include colon in timezone offset
#2647: Add `ObjectMapper.createParser()` and `createGenerator()` methods
- Add `SerializerProvider.findContentValueSerializer()` methods

2.10.4 (not yet released)
Expand Down
144 changes: 142 additions & 2 deletions src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -1175,8 +1175,148 @@ public JsonGenerator createGenerator(DataOutput out) throws IOException {
/**********************************************************
*/

// TODO

/**
* Factory method for constructing properly initialized {@link JsonParser}
* to read content from specified {@link File}.
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
* for properly closing it once content reading is complete.
*
* @since 2.11
*/
public JsonParser createParser(File src) throws IOException {
_assertNotNull("src", src);
return _jsonFactory.createParser(src);
}

/**
* Factory method for constructing properly initialized {@link JsonParser}
* to read content from specified {@link File}.
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
* for properly closing it once content reading is complete.
*
* @since 2.11
*/
public JsonParser createParser(URL src) throws IOException {
_assertNotNull("src", src);
return _jsonFactory.createParser(src);
}

/**
* Factory method for constructing properly initialized {@link JsonParser}
* to read content using specified {@link InputStream}.
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
* for properly closing it once content reading is complete.
*
* @since 2.11
*/
public JsonParser createParser(InputStream in) throws IOException {
_assertNotNull("in", in);
return _jsonFactory.createParser(in);
}

/**
* Factory method for constructing properly initialized {@link JsonParser}
* to read content using specified {@link Reader}.
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
* for properly closing it once content reading is complete.
*
* @since 2.11
*/
public JsonParser createParser(Reader r) throws IOException {
_assertNotNull("r", r);
return _jsonFactory.createParser(r);
}

/**
* Factory method for constructing properly initialized {@link JsonParser}
* to read content from specified byte array.
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
* for properly closing it once content reading is complete.
*
* @since 2.11
*/
public JsonParser createParser(byte[] content) throws IOException {
_assertNotNull("content", content);
return _jsonFactory.createParser(content);
}

/**
* Factory method for constructing properly initialized {@link JsonParser}
* to read content from specified byte array.
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
* for properly closing it once content reading is complete.
*
* @since 2.11
*/
public JsonParser createParser(byte[] content, int offset, int len) throws IOException {
_assertNotNull("content", content);
return _jsonFactory.createParser(content, offset, len);
}

/**
* Factory method for constructing properly initialized {@link JsonParser}
* to read content from specified String.
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
* for properly closing it once content reading is complete.
*
* @since 2.11
*/
public JsonParser createParser(String content) throws IOException {
_assertNotNull("content", content);
return _jsonFactory.createParser(content);
}

/**
* Factory method for constructing properly initialized {@link JsonParser}
* to read content from specified character array
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
* for properly closing it once content reading is complete.
*
* @since 2.11
*/
public JsonParser createParser(char[] content) throws IOException {
_assertNotNull("content", content);
return _jsonFactory.createParser(content);
}

/**
* Factory method for constructing properly initialized {@link JsonParser}
* to read content from specified character array.
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
* for properly closing it once content reading is complete.
*
* @since 2.11
*/
public JsonParser createParser(char[] content, int offset, int len) throws IOException {
_assertNotNull("content", content);
return _jsonFactory.createParser(content, offset, len);
}

/**
* Factory method for constructing properly initialized {@link JsonParser}
* to read content using specified {@link DataInput}.
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
* for properly closing it once content reading is complete.
*
* @since 2.11
*/
public JsonParser createParser(DataInput content) throws IOException {
_assertNotNull("content", content);
return _jsonFactory.createParser(content);
}

/**
* Factory method for constructing properly initialized {@link JsonParser}
* to read content using non-blocking (asynchronous) mode.
* Parser is not managed (or "owned") by ObjectMapper: caller is responsible
* for properly closing it once content reading is complete.
*
* @since 2.11
*/
public JsonParser createNonBlockingByteArrayParser() throws IOException {
return _jsonFactory.createNonBlockingByteArrayParser();
}

/*
/**********************************************************
/* Configuration: main config object access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static class POJO {
public void testSimpleViaParser() throws Exception
{
final String JSON = "[1]";
JsonParser p = MAPPER.getFactory().createParser(JSON);
JsonParser p = MAPPER.createParser(JSON);
Object ob = MAPPER.readerFor(Object.class)
.readValue(p);
p.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void testTokenBufferWithSequence() throws Exception
// [databind#2398]
public void testDeeplyNestedArrays() throws Exception
{
try (JsonParser p = MAPPER.tokenStreamFactory().createParser(_createNested(RECURSION_2398 * 2,
try (JsonParser p = MAPPER.createParser(_createNested(RECURSION_2398 * 2,
"[", " 123 ", "]"))) {
p.nextToken();
TokenBuffer b = new TokenBuffer(p);
Expand All @@ -124,7 +124,7 @@ public void testDeeplyNestedArrays() throws Exception

public void testDeeplyNestedObjects() throws Exception
{
try (JsonParser p = MAPPER.tokenStreamFactory().createParser(_createNested(RECURSION_2398,
try (JsonParser p = MAPPER.createParser(_createNested(RECURSION_2398,
"{\"a\":", "42", "}"))) {
p.nextToken();
TokenBuffer b = new TokenBuffer(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public void testSimple() throws Exception
// First "good" case with Strings
String JSON = "\"OK\" \"RULES\" null";
// multiple main-level mappings, need explicit parser:
JsonParser jp = MAPPER.getFactory().createParser(JSON);
JsonParser jp = MAPPER.createParser(JSON);

assertEquals(TestEnum.OK, MAPPER.readValue(jp, TestEnum.class));
assertEquals(TestEnum.RULES, MAPPER.readValue(jp, TestEnum.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ public void testSequenceOfInts() throws Exception
sb.append(" ");
sb.append(i);
}
JsonParser jp = MAPPER.getFactory().createParser(sb.toString());
JsonParser jp = MAPPER.createParser(sb.toString());
for (int i = 0; i < NR_OF_INTS; ++i) {
Integer result = MAPPER.readValue(jp, Integer.class);
assertEquals(Integer.valueOf(i), result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void testExceptionWithIncomplete()
throws Exception
{
BrokenStringReader r = new BrokenStringReader("[ 1, ", "TEST");
JsonParser p = MAPPER.getFactory().createParser(r);
JsonParser p = MAPPER.createParser(r);
try {
@SuppressWarnings("unused")
Object ob = MAPPER.readValue(p, Object.class);
Expand All @@ -90,7 +90,7 @@ public void testExceptionWithIncomplete()

public void testExceptionWithEOF() throws Exception
{
JsonParser p = MAPPER.getFactory().createParser(" 3");
JsonParser p = MAPPER.createParser(" 3");

Integer I = MAPPER.readValue(p, Integer.class);
assertEquals(3, I.intValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void testSimple() throws Exception
// to double-check [databind#1413]
public void testSimpleOther() throws Exception
{
JsonParser p = MAPPER.getFactory().createParser("{ }");
JsonParser p = MAPPER.createParser("{ }");
InvalidFormatException exc = InvalidFormatException.from(p, "Test", getClass(), String.class);
String json = MAPPER.writeValueAsString(exc);
p.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public class ParsingContext2525Test extends BaseMapTest

public void testAllWithRegularParser() throws Exception
{
try (JsonParser p = MAPPER.getFactory().createParser(MINIMAL_ARRAY_DOC)) {
try (JsonParser p = MAPPER.createParser(MINIMAL_ARRAY_DOC)) {
_testSimpleArrayUsingPathAsPointer(p);
}
try (JsonParser p = MAPPER.getFactory().createParser(MINIMAL_OBJECT_DOC)) {
try (JsonParser p = MAPPER.createParser(MINIMAL_OBJECT_DOC)) {
_testSimpleObjectUsingPathAsPointer(p);
}
try (JsonParser p = MAPPER.getFactory().createParser(FULL_DOC)) {
try (JsonParser p = MAPPER.createParser(FULL_DOC)) {
_testFullDocUsingPathAsPointer(p);
}
}
Expand Down Expand Up @@ -68,7 +68,7 @@ public void testFullDocWithBuffer() throws Exception

private TokenBuffer _readAsTokenBuffer(String doc) throws IOException
{
try (JsonParser p = MAPPER.getFactory().createParser(doc)) {
try (JsonParser p = MAPPER.createParser(doc)) {
p.nextToken();
return TokenBuffer.asCopyOfValue(p)
.overrideParentContext(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,66 +25,66 @@ public class EmptyContentAsTreeTest extends BaseMapTest

public void testNullFromEOFWithParserAndMapper() throws Exception
{
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY0)) {
try (JsonParser p = MAPPER.createParser(EMPTY0)) {
_assertNullTree(MAPPER.readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1)) {
try (JsonParser p = MAPPER.createParser(EMPTY1)) {
_assertNullTree(MAPPER.readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(new StringReader(EMPTY0))) {
try (JsonParser p = MAPPER.createParser(new StringReader(EMPTY0))) {
_assertNullTree(MAPPER.readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(new StringReader(EMPTY1))) {
try (JsonParser p = MAPPER.createParser(new StringReader(EMPTY1))) {
_assertNullTree(MAPPER.readTree(p));
}

try (JsonParser p = MAPPER.getFactory().createParser(EMPTY0_BYTES)) {
try (JsonParser p = MAPPER.createParser(EMPTY0_BYTES)) {
_assertNullTree(MAPPER.readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1_BYTES)) {
try (JsonParser p = MAPPER.createParser(EMPTY1_BYTES)) {
_assertNullTree(MAPPER.readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1_BYTES, 0, EMPTY1_BYTES.length)) {
try (JsonParser p = MAPPER.createParser(EMPTY1_BYTES, 0, EMPTY1_BYTES.length)) {
_assertNullTree(MAPPER.readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(new ByteArrayInputStream(EMPTY0_BYTES))) {
try (JsonParser p = MAPPER.createParser(new ByteArrayInputStream(EMPTY0_BYTES))) {
_assertNullTree(MAPPER.readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(new ByteArrayInputStream(EMPTY1_BYTES))) {
try (JsonParser p = MAPPER.createParser(new ByteArrayInputStream(EMPTY1_BYTES))) {
_assertNullTree(MAPPER.readTree(p));
}
}

// [databind#1406]
public void testNullFromEOFWithParserAndReader() throws Exception
{
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY0)) {
try (JsonParser p = MAPPER.createParser(EMPTY0)) {
_assertNullTree(MAPPER.reader().readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1)) {
try (JsonParser p = MAPPER.createParser(EMPTY1)) {
_assertNullTree(MAPPER.reader().readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(new StringReader(EMPTY0))) {
try (JsonParser p = MAPPER.createParser(new StringReader(EMPTY0))) {
_assertNullTree(MAPPER.reader().readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(new StringReader(EMPTY1))) {
try (JsonParser p = MAPPER.createParser(new StringReader(EMPTY1))) {
_assertNullTree(MAPPER.reader().readTree(p));
}

try (JsonParser p = MAPPER.getFactory().createParser(EMPTY0_BYTES)) {
try (JsonParser p = MAPPER.createParser(EMPTY0_BYTES)) {
_assertNullTree(MAPPER.reader().readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1_BYTES)) {
try (JsonParser p = MAPPER.createParser(EMPTY1_BYTES)) {
_assertNullTree(MAPPER.reader().readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1_BYTES, 0, EMPTY1_BYTES.length)) {
try (JsonParser p = MAPPER.createParser(EMPTY1_BYTES, 0, EMPTY1_BYTES.length)) {
_assertNullTree(MAPPER.reader().readTree(p));
}

try (JsonParser p = MAPPER.getFactory().createParser(new ByteArrayInputStream(EMPTY0_BYTES))) {
try (JsonParser p = MAPPER.createParser(new ByteArrayInputStream(EMPTY0_BYTES))) {
_assertNullTree(MAPPER.reader().readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(new ByteArrayInputStream(EMPTY1_BYTES))) {
try (JsonParser p = MAPPER.createParser(new ByteArrayInputStream(EMPTY1_BYTES))) {
_assertNullTree(MAPPER.reader().readTree(p));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class TreeFromIncompleteJsonTest extends BaseMapTest
public void testErrorHandling() throws IOException {

String json = "{\"A\":{\"B\":\n";
JsonParser parser = MAPPER.getFactory().createParser(json);
JsonParser parser = MAPPER.createParser(json);
try {
parser.readValueAsTree();
} catch (JsonEOFException e) {
Expand Down
Loading

0 comments on commit 0404c48

Please sign in to comment.