Skip to content

Commit e052c31

Browse files
committed
Fix #296
1 parent 4601785 commit e052c31

File tree

13 files changed

+80
-41
lines changed

13 files changed

+80
-41
lines changed

release-notes/CREDITS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,7 @@ Tanguy Leroux (tlrx@github)
9393
Mikael Staldal (mikaelstaldal@github)
9494
* Contributed fix for #265: `JsonStringEncoder` should allow passing `CharSequence`
9595
(2.8.0)
96+
97+
Kevin Gallardo (newkek@github)
98+
* Reported #296: JsonParserSequence skips a token on a switched Parser
99+
(2.8.0)

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ JSON library.
3939
#290: Add `JsonGenerator.canWriteFormattedNumbers()` for introspection
4040
#294: Add `JsonGenerator.writeFieldId(long)` method to support binary formats
4141
with non-String keys
42+
#296: JsonParserSequence skips a token on a switched Parser
43+
(reported by Kevin G)
4244
- Add `JsonParser.currentToken()` and `JsonParser.currentTokenId()` as replacements
4345
for `getCurrentToken()` and `getCurrentTokenId()`, respectively. Existing methods
4446
will likely be deprecated in 2.9.

src/main/java/com/fasterxml/jackson/core/JsonGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,7 @@ public void writeOmittedField(String fieldName) throws IOException { }
16811681
*/
16821682
public void copyCurrentEvent(JsonParser p) throws IOException
16831683
{
1684-
JsonToken t = p.getCurrentToken();
1684+
JsonToken t = p.currentToken();
16851685
// sanity check; what to do?
16861686
if (t == null) {
16871687
_reportError("No current event to copy");
@@ -1784,7 +1784,7 @@ public void copyCurrentEvent(JsonParser p) throws IOException
17841784
*/
17851785
public void copyCurrentStructure(JsonParser p) throws IOException
17861786
{
1787-
JsonToken t = p.getCurrentToken();
1787+
JsonToken t = p.currentToken();
17881788
if (t == null) {
17891789
_reportError("No current event to copy");
17901790
}

src/main/java/com/fasterxml/jackson/core/JsonParser.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ public int currentTokenId() {
884884
/**
885885
* Method that is functionally equivalent to:
886886
*<code>
887-
* return getCurrentTokenId() == id
887+
* return currentTokenId() == id
888888
*</code>
889889
* but may be more efficiently implemented.
890890
*<p>
@@ -899,7 +899,7 @@ public int currentTokenId() {
899899
/**
900900
* Method that is functionally equivalent to:
901901
*<code>
902-
* return getCurrentTokenId() == id
902+
* return currentToken() == t
903903
*</code>
904904
* but may be more efficiently implemented.
905905
*<p>
@@ -956,23 +956,23 @@ public int currentTokenId() {
956956
*<p>
957957
* Default implementation is equivalent to:
958958
*<pre>
959-
* getCurrentToken() == JsonToken.START_ARRAY
959+
* currentToken() == JsonToken.START_ARRAY
960960
*</pre>
961961
* but may be overridden by custom parser implementations.
962962
*
963963
* @return True if the current token can be considered as a
964964
* start-array marker (such {@link JsonToken#START_ARRAY});
965965
* false if not.
966966
*/
967-
public boolean isExpectedStartArrayToken() { return getCurrentToken() == JsonToken.START_ARRAY; }
967+
public boolean isExpectedStartArrayToken() { return currentToken() == JsonToken.START_ARRAY; }
968968

969969
/**
970970
* Similar to {@link #isExpectedStartArrayToken()}, but checks whether stream
971971
* currently points to {@link JsonToken#START_OBJECT}.
972-
*
972+
*
973973
* @since 2.5
974974
*/
975-
public boolean isExpectedStartObjectToken() { return getCurrentToken() == JsonToken.START_OBJECT; }
975+
public boolean isExpectedStartObjectToken() { return currentToken() == JsonToken.START_OBJECT; }
976976

977977
/*
978978
/**********************************************************
@@ -1285,12 +1285,12 @@ public short getShortValue() throws IOException
12851285
* may be thrown to indicate numeric overflow/underflow.
12861286
*/
12871287
public boolean getBooleanValue() throws IOException {
1288-
JsonToken t = getCurrentToken();
1288+
JsonToken t = currentToken();
12891289
if (t == JsonToken.VALUE_TRUE) return true;
12901290
if (t == JsonToken.VALUE_FALSE) return false;
12911291
throw new JsonParseException(this,
1292-
String.format("Current token (%s) not of boolean type", t))
1293-
.withRequestPayload(_requestPayload);
1292+
String.format("Current token (%s) not of boolean type", t))
1293+
.withRequestPayload(_requestPayload);
12941294
}
12951295

12961296
/**

src/main/java/com/fasterxml/jackson/core/filter/FilteringParserDelegate.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,16 @@ public int getMatchCount() {
146146
*/
147147

148148
@Override public JsonToken getCurrentToken() { return _currToken; }
149+
@Override public JsonToken currentToken() { return _currToken; }
149150

150151
@Override public final int getCurrentTokenId() {
151152
final JsonToken t = _currToken;
152153
return (t == null) ? JsonTokenId.ID_NO_TOKEN : t.id();
153154
}
155+
@Override public final int currentTokenId() {
156+
final JsonToken t = _currToken;
157+
return (t == null) ? JsonTokenId.ID_NO_TOKEN : t.id();
158+
}
154159

155160
@Override public boolean hasCurrentToken() { return _currToken != null; }
156161
@Override public boolean hasTokenId(int id) {

src/main/java/com/fasterxml/jackson/core/util/JsonParserSequence.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ public class JsonParserSequence extends JsonParserDelegate
2525
* Index of the next parser in {@link #_parsers}.
2626
*/
2727
protected int _nextParser;
28+
29+
/**
30+
* Flag used to indicate that `JsonParser.nextToken()` should not be called,
31+
* due to parser already pointing to a token.
32+
*
33+
* @since 2.8
34+
*/
35+
protected boolean _suppressNextToken;
2836

2937
/*
3038
*******************************************************
@@ -35,6 +43,7 @@ public class JsonParserSequence extends JsonParserDelegate
3543
protected JsonParserSequence(JsonParser[] parsers)
3644
{
3745
super(parsers[0]);
46+
_suppressNextToken = delegate.hasCurrentToken();
3847
_parsers = parsers;
3948
_nextParser = 1;
4049
}
@@ -94,15 +103,21 @@ public void close() throws IOException {
94103
}
95104

96105
@Override
97-
public JsonToken nextToken() throws IOException, JsonParseException
106+
public JsonToken nextToken() throws IOException
98107
{
108+
if (delegate == null) {
109+
return null;
110+
}
111+
if (_suppressNextToken) {
112+
_suppressNextToken = false;
113+
return delegate.currentToken();
114+
}
99115
JsonToken t = delegate.nextToken();
100-
if (t != null) return t;
101-
while (switchToNext()) {
102-
t = delegate.nextToken();
103-
if (t != null) return t;
116+
while ((t == null) && switchToNext()) {
117+
t = delegate.hasCurrentToken()
118+
? delegate.currentToken() : delegate.nextToken();
104119
}
105-
return null;
120+
return t;
106121
}
107122

108123
/*

src/test/java/com/fasterxml/jackson/core/json/ParserSequenceTest.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.core.*;
44
import com.fasterxml.jackson.core.util.JsonParserSequence;
55

6+
@SuppressWarnings("resource")
67
public class ParserSequenceTest
78
extends com.fasterxml.jackson.core.BaseTest
89
{
@@ -43,8 +44,22 @@ public void testSimple() throws Exception
4344
assertTrue(seq.isClosed());
4445

4546
seq.close();
46-
// redundant, but call to remove IDE warnings
47-
p1.close();
48-
p2.close();
47+
}
48+
49+
// for [jackson-core#296]
50+
public void testInitialized() throws Exception
51+
{
52+
JsonParser p1 = JSON_FACTORY.createParser("1 2");
53+
JsonParser p2 = JSON_FACTORY.createParser("3 false");
54+
// consume '1', move to '2'
55+
assertToken(JsonToken.VALUE_NUMBER_INT, p1.nextToken());
56+
assertToken(JsonToken.VALUE_NUMBER_INT, p1.nextToken());
57+
58+
JsonParserSequence seq = JsonParserSequence.createFlattened(p1, p2);
59+
assertToken(JsonToken.VALUE_NUMBER_INT, seq.nextToken());
60+
assertEquals(2, seq.getIntValue());
61+
assertToken(JsonToken.VALUE_NUMBER_INT, seq.nextToken());
62+
assertEquals(3, seq.getIntValue());
63+
seq.close();
4964
}
5065
}

src/test/java/com/fasterxml/jackson/core/json/StringGenerationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ private void _testLongerRandomMulti(int readMode, String text, boolean charArray
287287
}
288288
offset += act.length();
289289
}
290-
assertEquals(JsonToken.END_ARRAY, p.getCurrentToken());
290+
assertEquals(JsonToken.END_ARRAY, p.currentToken());
291291
p.close();
292292
}
293293
}

src/test/java/com/fasterxml/jackson/core/json/TestParserOverrides.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ public void _testTokenAccess(JsonFactory jf, boolean useStream) throws Exception
4545
JsonParser jp = useStream ?
4646
jf.createParser(new ByteArrayInputStream(DOC.getBytes("UTF-8")))
4747
: jf.createParser(DOC);
48-
assertNull(jp.getCurrentToken());
48+
assertNull(jp.currentToken());
4949
jp.clearCurrentToken();
50-
assertNull(jp.getCurrentToken());
50+
assertNull(jp.currentToken());
5151
assertNull(jp.getEmbeddedObject());
5252
assertToken(JsonToken.START_ARRAY, jp.nextToken());
53-
assertToken(JsonToken.START_ARRAY, jp.getCurrentToken());
53+
assertToken(JsonToken.START_ARRAY, jp.currentToken());
5454
jp.clearCurrentToken();
55-
assertNull(jp.getCurrentToken());
55+
assertNull(jp.currentToken());
5656
// Also: no codec defined by default
5757
try {
5858
jp.readValueAsTree();
@@ -69,7 +69,7 @@ private void _testCurrentName(JsonFactory jf, boolean useStream) throws Exceptio
6969
JsonParser jp = useStream ?
7070
jf.createParser(new ByteArrayInputStream(DOC.getBytes("UTF-8")))
7171
: jf.createParser(new StringReader(DOC));
72-
assertNull(jp.getCurrentToken());
72+
assertNull(jp.currentToken());
7373
assertToken(JsonToken.START_OBJECT, jp.nextToken());
7474
assertToken(JsonToken.FIELD_NAME, jp.nextToken());
7575
assertEquals("first", jp.getCurrentName());
@@ -94,9 +94,7 @@ private void _testCurrentName(JsonFactory jf, boolean useStream) throws Exceptio
9494

9595
assertToken(JsonToken.END_OBJECT, jp.nextToken());
9696
jp.clearCurrentToken();
97-
assertNull(jp.getCurrentToken());
97+
assertNull(jp.currentToken());
9898
jp.close();
9999
}
100-
101-
102100
}

src/test/java/com/fasterxml/jackson/core/json/TestRootValues.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private void _testBrokeanNumber(boolean useStream) throws Exception
4545
// Should fail, right away
4646
try {
4747
p.nextToken();
48-
fail("Ought to fail! Instead, got token: "+p.getCurrentToken());
48+
fail("Ought to fail! Instead, got token: "+p.currentToken());
4949
} catch (JsonParseException e) {
5050
verifyException(e, "unexpected character");
5151
}

src/test/java/com/fasterxml/jackson/core/main/TestGeneratorCopy.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void testCopyRootTokens()
2626
while ((t = jp.nextToken()) != null) {
2727
gen.copyCurrentEvent(jp);
2828
// should not change parser state:
29-
assertToken(t, jp.getCurrentToken());
29+
assertToken(t, jp.currentToken());
3030
}
3131
jp.close();
3232
gen.close();
@@ -46,14 +46,14 @@ public void testCopyArrayTokens()
4646
assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
4747
gen.copyCurrentEvent(jp);
4848
// should not change parser state:
49-
assertToken(JsonToken.VALUE_NUMBER_INT, jp.getCurrentToken());
49+
assertToken(JsonToken.VALUE_NUMBER_INT, jp.currentToken());
5050
assertEquals(123, jp.getIntValue());
5151

5252
// And then let's copy the array
5353
assertToken(JsonToken.START_ARRAY, jp.nextToken());
5454
gen.copyCurrentStructure(jp);
5555
// which will advance parser to matching close Array
56-
assertToken(JsonToken.END_ARRAY, jp.getCurrentToken());
56+
assertToken(JsonToken.END_ARRAY, jp.currentToken());
5757
jp.close();
5858
gen.close();
5959

@@ -72,7 +72,7 @@ public void testCopyObjectTokens()
7272
assertToken(JsonToken.START_OBJECT, jp.nextToken());
7373
gen.copyCurrentStructure(jp);
7474
// which will advance parser to matching end Object
75-
assertToken(JsonToken.END_OBJECT, jp.getCurrentToken());
75+
assertToken(JsonToken.END_OBJECT, jp.currentToken());
7676
jp.close();
7777
gen.close();
7878

src/test/java/com/fasterxml/jackson/core/read/JsonParserTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ private void _testSkipping(int mode) throws Exception
226226
// First, skipping of the whole thing
227227
assertToken(JsonToken.START_ARRAY, p.nextToken());
228228
p.skipChildren();
229-
assertEquals(JsonToken.END_ARRAY, p.getCurrentToken());
229+
assertEquals(JsonToken.END_ARRAY, p.currentToken());
230230
if (!isInputData) {
231231
JsonToken t = p.nextToken();
232232
if (t != null) {
@@ -242,27 +242,27 @@ private void _testSkipping(int mode) throws Exception
242242
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
243243
p.skipChildren();
244244
// shouldn't move
245-
assertToken(JsonToken.VALUE_NUMBER_INT, p.getCurrentToken());
245+
assertToken(JsonToken.VALUE_NUMBER_INT, p.currentToken());
246246
assertEquals(1, p.getIntValue());
247247

248248
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
249249
// then skip array
250250
assertToken(JsonToken.START_ARRAY, p.nextToken());
251251
p.skipChildren();
252-
assertToken(JsonToken.END_ARRAY, p.getCurrentToken());
252+
assertToken(JsonToken.END_ARRAY, p.currentToken());
253253

254254
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
255255
assertToken(JsonToken.START_OBJECT, p.nextToken());
256256
p.skipChildren();
257-
assertToken(JsonToken.END_OBJECT, p.getCurrentToken());
257+
assertToken(JsonToken.END_OBJECT, p.currentToken());
258258

259259
assertToken(JsonToken.START_ARRAY, p.nextToken());
260260
p.skipChildren();
261-
assertToken(JsonToken.END_ARRAY, p.getCurrentToken());
261+
assertToken(JsonToken.END_ARRAY, p.currentToken());
262262

263263
assertToken(JsonToken.START_OBJECT, p.nextToken());
264264
p.skipChildren();
265-
assertToken(JsonToken.END_OBJECT, p.getCurrentToken());
265+
assertToken(JsonToken.END_OBJECT, p.currentToken());
266266

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

src/test/java/com/fasterxml/jackson/core/util/TestDelegates.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void testParserDelegate() throws IOException
1818
JsonParser parser = JSON_F.createParser("[ 1, true, null, { } ]");
1919
JsonParserDelegate del = new JsonParserDelegate(parser);
2020

21-
assertNull(del.getCurrentToken());
21+
assertNull(del.currentToken());
2222
assertToken(JsonToken.START_ARRAY, del.nextToken());
2323
assertEquals("[", del.getText());
2424
assertToken(JsonToken.VALUE_NUMBER_INT, del.nextToken());

0 commit comments

Comments
 (0)