Skip to content

Commit

Permalink
Fix #296
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jul 2, 2016
1 parent 4601785 commit e052c31
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 41 deletions.
4 changes: 4 additions & 0 deletions release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,7 @@ Tanguy Leroux (tlrx@github)
Mikael Staldal (mikaelstaldal@github)
* Contributed fix for #265: `JsonStringEncoder` should allow passing `CharSequence`
(2.8.0)

Kevin Gallardo (newkek@github)
* Reported #296: JsonParserSequence skips a token on a switched Parser
(2.8.0)
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ JSON library.
#290: Add `JsonGenerator.canWriteFormattedNumbers()` for introspection
#294: Add `JsonGenerator.writeFieldId(long)` method to support binary formats
with non-String keys
#296: JsonParserSequence skips a token on a switched Parser
(reported by Kevin G)
- Add `JsonParser.currentToken()` and `JsonParser.currentTokenId()` as replacements
for `getCurrentToken()` and `getCurrentTokenId()`, respectively. Existing methods
will likely be deprecated in 2.9.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/fasterxml/jackson/core/JsonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1681,7 +1681,7 @@ public void writeOmittedField(String fieldName) throws IOException { }
*/
public void copyCurrentEvent(JsonParser p) throws IOException
{
JsonToken t = p.getCurrentToken();
JsonToken t = p.currentToken();
// sanity check; what to do?
if (t == null) {
_reportError("No current event to copy");
Expand Down Expand Up @@ -1784,7 +1784,7 @@ public void copyCurrentEvent(JsonParser p) throws IOException
*/
public void copyCurrentStructure(JsonParser p) throws IOException
{
JsonToken t = p.getCurrentToken();
JsonToken t = p.currentToken();
if (t == null) {
_reportError("No current event to copy");
}
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/fasterxml/jackson/core/JsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ public int currentTokenId() {
/**
* Method that is functionally equivalent to:
*<code>
* return getCurrentTokenId() == id
* return currentTokenId() == id
*</code>
* but may be more efficiently implemented.
*<p>
Expand All @@ -899,7 +899,7 @@ public int currentTokenId() {
/**
* Method that is functionally equivalent to:
*<code>
* return getCurrentTokenId() == id
* return currentToken() == t
*</code>
* but may be more efficiently implemented.
*<p>
Expand Down Expand Up @@ -956,23 +956,23 @@ public int currentTokenId() {
*<p>
* Default implementation is equivalent to:
*<pre>
* getCurrentToken() == JsonToken.START_ARRAY
* currentToken() == JsonToken.START_ARRAY
*</pre>
* but may be overridden by custom parser implementations.
*
* @return True if the current token can be considered as a
* start-array marker (such {@link JsonToken#START_ARRAY});
* false if not.
*/
public boolean isExpectedStartArrayToken() { return getCurrentToken() == JsonToken.START_ARRAY; }
public boolean isExpectedStartArrayToken() { return currentToken() == JsonToken.START_ARRAY; }

/**
* Similar to {@link #isExpectedStartArrayToken()}, but checks whether stream
* currently points to {@link JsonToken#START_OBJECT}.
*
*
* @since 2.5
*/
public boolean isExpectedStartObjectToken() { return getCurrentToken() == JsonToken.START_OBJECT; }
public boolean isExpectedStartObjectToken() { return currentToken() == JsonToken.START_OBJECT; }

/*
/**********************************************************
Expand Down Expand Up @@ -1285,12 +1285,12 @@ public short getShortValue() throws IOException
* may be thrown to indicate numeric overflow/underflow.
*/
public boolean getBooleanValue() throws IOException {
JsonToken t = getCurrentToken();
JsonToken t = currentToken();
if (t == JsonToken.VALUE_TRUE) return true;
if (t == JsonToken.VALUE_FALSE) return false;
throw new JsonParseException(this,
String.format("Current token (%s) not of boolean type", t))
.withRequestPayload(_requestPayload);
String.format("Current token (%s) not of boolean type", t))
.withRequestPayload(_requestPayload);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,16 @@ public int getMatchCount() {
*/

@Override public JsonToken getCurrentToken() { return _currToken; }
@Override public JsonToken currentToken() { return _currToken; }

@Override public final int getCurrentTokenId() {
final JsonToken t = _currToken;
return (t == null) ? JsonTokenId.ID_NO_TOKEN : t.id();
}
@Override public final int currentTokenId() {
final JsonToken t = _currToken;
return (t == null) ? JsonTokenId.ID_NO_TOKEN : t.id();
}

@Override public boolean hasCurrentToken() { return _currToken != null; }
@Override public boolean hasTokenId(int id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public class JsonParserSequence extends JsonParserDelegate
* Index of the next parser in {@link #_parsers}.
*/
protected int _nextParser;

/**
* Flag used to indicate that `JsonParser.nextToken()` should not be called,
* due to parser already pointing to a token.
*
* @since 2.8
*/
protected boolean _suppressNextToken;

/*
*******************************************************
Expand All @@ -35,6 +43,7 @@ public class JsonParserSequence extends JsonParserDelegate
protected JsonParserSequence(JsonParser[] parsers)
{
super(parsers[0]);
_suppressNextToken = delegate.hasCurrentToken();
_parsers = parsers;
_nextParser = 1;
}
Expand Down Expand Up @@ -94,15 +103,21 @@ public void close() throws IOException {
}

@Override
public JsonToken nextToken() throws IOException, JsonParseException
public JsonToken nextToken() throws IOException
{
if (delegate == null) {
return null;
}
if (_suppressNextToken) {
_suppressNextToken = false;
return delegate.currentToken();
}
JsonToken t = delegate.nextToken();
if (t != null) return t;
while (switchToNext()) {
t = delegate.nextToken();
if (t != null) return t;
while ((t == null) && switchToNext()) {
t = delegate.hasCurrentToken()
? delegate.currentToken() : delegate.nextToken();
}
return null;
return t;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.util.JsonParserSequence;

@SuppressWarnings("resource")
public class ParserSequenceTest
extends com.fasterxml.jackson.core.BaseTest
{
Expand Down Expand Up @@ -43,8 +44,22 @@ public void testSimple() throws Exception
assertTrue(seq.isClosed());

seq.close();
// redundant, but call to remove IDE warnings
p1.close();
p2.close();
}

// for [jackson-core#296]
public void testInitialized() throws Exception
{
JsonParser p1 = JSON_FACTORY.createParser("1 2");
JsonParser p2 = JSON_FACTORY.createParser("3 false");
// consume '1', move to '2'
assertToken(JsonToken.VALUE_NUMBER_INT, p1.nextToken());
assertToken(JsonToken.VALUE_NUMBER_INT, p1.nextToken());

JsonParserSequence seq = JsonParserSequence.createFlattened(p1, p2);
assertToken(JsonToken.VALUE_NUMBER_INT, seq.nextToken());
assertEquals(2, seq.getIntValue());
assertToken(JsonToken.VALUE_NUMBER_INT, seq.nextToken());
assertEquals(3, seq.getIntValue());
seq.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private void _testLongerRandomMulti(int readMode, String text, boolean charArray
}
offset += act.length();
}
assertEquals(JsonToken.END_ARRAY, p.getCurrentToken());
assertEquals(JsonToken.END_ARRAY, p.currentToken());
p.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ public void _testTokenAccess(JsonFactory jf, boolean useStream) throws Exception
JsonParser jp = useStream ?
jf.createParser(new ByteArrayInputStream(DOC.getBytes("UTF-8")))
: jf.createParser(DOC);
assertNull(jp.getCurrentToken());
assertNull(jp.currentToken());
jp.clearCurrentToken();
assertNull(jp.getCurrentToken());
assertNull(jp.currentToken());
assertNull(jp.getEmbeddedObject());
assertToken(JsonToken.START_ARRAY, jp.nextToken());
assertToken(JsonToken.START_ARRAY, jp.getCurrentToken());
assertToken(JsonToken.START_ARRAY, jp.currentToken());
jp.clearCurrentToken();
assertNull(jp.getCurrentToken());
assertNull(jp.currentToken());
// Also: no codec defined by default
try {
jp.readValueAsTree();
Expand All @@ -69,7 +69,7 @@ private void _testCurrentName(JsonFactory jf, boolean useStream) throws Exceptio
JsonParser jp = useStream ?
jf.createParser(new ByteArrayInputStream(DOC.getBytes("UTF-8")))
: jf.createParser(new StringReader(DOC));
assertNull(jp.getCurrentToken());
assertNull(jp.currentToken());
assertToken(JsonToken.START_OBJECT, jp.nextToken());
assertToken(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("first", jp.getCurrentName());
Expand All @@ -94,9 +94,7 @@ private void _testCurrentName(JsonFactory jf, boolean useStream) throws Exceptio

assertToken(JsonToken.END_OBJECT, jp.nextToken());
jp.clearCurrentToken();
assertNull(jp.getCurrentToken());
assertNull(jp.currentToken());
jp.close();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private void _testBrokeanNumber(boolean useStream) throws Exception
// Should fail, right away
try {
p.nextToken();
fail("Ought to fail! Instead, got token: "+p.getCurrentToken());
fail("Ought to fail! Instead, got token: "+p.currentToken());
} catch (JsonParseException e) {
verifyException(e, "unexpected character");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void testCopyRootTokens()
while ((t = jp.nextToken()) != null) {
gen.copyCurrentEvent(jp);
// should not change parser state:
assertToken(t, jp.getCurrentToken());
assertToken(t, jp.currentToken());
}
jp.close();
gen.close();
Expand All @@ -46,14 +46,14 @@ public void testCopyArrayTokens()
assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
gen.copyCurrentEvent(jp);
// should not change parser state:
assertToken(JsonToken.VALUE_NUMBER_INT, jp.getCurrentToken());
assertToken(JsonToken.VALUE_NUMBER_INT, jp.currentToken());
assertEquals(123, jp.getIntValue());

// And then let's copy the array
assertToken(JsonToken.START_ARRAY, jp.nextToken());
gen.copyCurrentStructure(jp);
// which will advance parser to matching close Array
assertToken(JsonToken.END_ARRAY, jp.getCurrentToken());
assertToken(JsonToken.END_ARRAY, jp.currentToken());
jp.close();
gen.close();

Expand All @@ -72,7 +72,7 @@ public void testCopyObjectTokens()
assertToken(JsonToken.START_OBJECT, jp.nextToken());
gen.copyCurrentStructure(jp);
// which will advance parser to matching end Object
assertToken(JsonToken.END_OBJECT, jp.getCurrentToken());
assertToken(JsonToken.END_OBJECT, jp.currentToken());
jp.close();
gen.close();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private void _testSkipping(int mode) throws Exception
// First, skipping of the whole thing
assertToken(JsonToken.START_ARRAY, p.nextToken());
p.skipChildren();
assertEquals(JsonToken.END_ARRAY, p.getCurrentToken());
assertEquals(JsonToken.END_ARRAY, p.currentToken());
if (!isInputData) {
JsonToken t = p.nextToken();
if (t != null) {
Expand All @@ -242,27 +242,27 @@ private void _testSkipping(int mode) throws Exception
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
p.skipChildren();
// shouldn't move
assertToken(JsonToken.VALUE_NUMBER_INT, p.getCurrentToken());
assertToken(JsonToken.VALUE_NUMBER_INT, p.currentToken());
assertEquals(1, p.getIntValue());

assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
// then skip array
assertToken(JsonToken.START_ARRAY, p.nextToken());
p.skipChildren();
assertToken(JsonToken.END_ARRAY, p.getCurrentToken());
assertToken(JsonToken.END_ARRAY, p.currentToken());

assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertToken(JsonToken.START_OBJECT, p.nextToken());
p.skipChildren();
assertToken(JsonToken.END_OBJECT, p.getCurrentToken());
assertToken(JsonToken.END_OBJECT, p.currentToken());

assertToken(JsonToken.START_ARRAY, p.nextToken());
p.skipChildren();
assertToken(JsonToken.END_ARRAY, p.getCurrentToken());
assertToken(JsonToken.END_ARRAY, p.currentToken());

assertToken(JsonToken.START_OBJECT, p.nextToken());
p.skipChildren();
assertToken(JsonToken.END_OBJECT, p.getCurrentToken());
assertToken(JsonToken.END_OBJECT, p.currentToken());

assertToken(JsonToken.END_ARRAY, p.nextToken());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void testParserDelegate() throws IOException
JsonParser parser = JSON_F.createParser("[ 1, true, null, { } ]");
JsonParserDelegate del = new JsonParserDelegate(parser);

assertNull(del.getCurrentToken());
assertNull(del.currentToken());
assertToken(JsonToken.START_ARRAY, del.nextToken());
assertEquals("[", del.getText());
assertToken(JsonToken.VALUE_NUMBER_INT, del.nextToken());
Expand Down

0 comments on commit e052c31

Please sign in to comment.