Skip to content

Commit 4195e6e

Browse files
committed
Fix #229
1 parent 329756d commit 4195e6e

File tree

4 files changed

+65
-34
lines changed

4 files changed

+65
-34
lines changed

release-notes/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ JSON library.
2020
(via `JsonParseException`, `JsonGenerationException`)
2121
#211: Typo of function name com.fasterxml.jackson.core.Version.isUknownVersion()
2222
(reported by timray@github)
23+
#229: Array element and field token spans include previous comma.
2324
- Implemented `ReaderBasedJsonParser.nextFieldName(SerializableString)`
2425
(to improved Afterburner performance over String/char[] sources)
2526

src/main/java/com/fasterxml/jackson/core/json/ReaderBasedJsonParser.java

+28-15
Original file line numberDiff line numberDiff line change
@@ -579,26 +579,20 @@ public final JsonToken nextToken() throws IOException
579579
close();
580580
return (_currToken = null);
581581
}
582-
583-
/* First, need to ensure we know the starting location of token
584-
* after skipping leading white space
585-
*/
586-
_tokenInputTotal = _currInputProcessed + _inputPtr - 1;
587-
_tokenInputRow = _currInputRow;
588-
_tokenInputCol = _inputPtr - _currInputRowStart - 1;
589-
590-
// finally: clear any data retained so far
582+
// clear any data retained so far
591583
_binaryValue = null;
592584

593585
// Closing scope?
594586
if (i == INT_RBRACKET) {
587+
_updateLocationFromInputPtr();
595588
if (!_parsingContext.inArray()) {
596589
_reportMismatchedEndMarker(i, '}');
597590
}
598591
_parsingContext = _parsingContext.getParent();
599592
return (_currToken = JsonToken.END_ARRAY);
600593
}
601594
if (i == INT_RCURLY) {
595+
_updateLocationFromInputPtr();
602596
if (!_parsingContext.inObject()) {
603597
_reportMismatchedEndMarker(i, ']');
604598
}
@@ -610,6 +604,7 @@ public final JsonToken nextToken() throws IOException
610604
if (_parsingContext.expectComma()) {
611605
i = _skipComma(i);
612606
}
607+
_updateLocationFromInputPtr();
613608

614609
/* And should we now have a name? Always true for
615610
* Object contexts, since the intermediate 'expect-value'
@@ -700,6 +695,9 @@ private final JsonToken _nextAfterName()
700695
_nameCopied = false; // need to invalidate if it was copied
701696
JsonToken t = _nextToken;
702697
_nextToken = null;
698+
699+
// !!! 16-Nov-2015, tatu: TODO: fix [databind#37], copy next location to current here
700+
703701
// Also: may need to start new context?
704702
if (t == JsonToken.START_ARRAY) {
705703
_parsingContext = _parsingContext.createChildArrayContext(_tokenInputRow, _tokenInputCol);
@@ -735,11 +733,10 @@ public boolean nextFieldName(SerializableString sstr) throws IOException
735733
_currToken = null;
736734
return false;
737735
}
738-
_tokenInputTotal = _currInputProcessed + _inputPtr - 1;
739-
_tokenInputRow = _currInputRow;
740-
_tokenInputCol = _inputPtr - _currInputRowStart - 1;
741736
_binaryValue = null;
737+
742738
if (i == INT_RBRACKET) {
739+
_updateLocationFromInputPtr();
743740
if (!_parsingContext.inArray()) {
744741
_reportMismatchedEndMarker(i, '}');
745742
}
@@ -748,6 +745,7 @@ public boolean nextFieldName(SerializableString sstr) throws IOException
748745
return false;
749746
}
750747
if (i == INT_RCURLY) {
748+
_updateLocationFromInputPtr();
751749
if (!_parsingContext.inObject()) {
752750
_reportMismatchedEndMarker(i, ']');
753751
}
@@ -758,6 +756,7 @@ public boolean nextFieldName(SerializableString sstr) throws IOException
758756
if (_parsingContext.expectComma()) {
759757
i = _skipComma(i);
760758
}
759+
_updateLocationFromInputPtr();
761760

762761
if (!_parsingContext.inObject()) {
763762
_nextTokenNotInObject(i);
@@ -813,11 +812,9 @@ public String nextFieldName() throws IOException
813812
_currToken = null;
814813
return null;
815814
}
816-
_tokenInputTotal = _currInputProcessed + _inputPtr - 1;
817-
_tokenInputRow = _currInputRow;
818-
_tokenInputCol = _inputPtr - _currInputRowStart - 1;
819815
_binaryValue = null;
820816
if (i == INT_RBRACKET) {
817+
_updateLocationFromInputPtr();
821818
if (!_parsingContext.inArray()) {
822819
_reportMismatchedEndMarker(i, '}');
823820
}
@@ -826,6 +823,7 @@ public String nextFieldName() throws IOException
826823
return null;
827824
}
828825
if (i == INT_RCURLY) {
826+
_updateLocationFromInputPtr();
829827
if (!_parsingContext.inObject()) {
830828
_reportMismatchedEndMarker(i, ']');
831829
}
@@ -837,6 +835,7 @@ public String nextFieldName() throws IOException
837835
i = _skipComma(i);
838836
}
839837

838+
_updateLocationFromInputPtr();
840839
if (!_parsingContext.inObject()) {
841840
_nextTokenNotInObject(i);
842841
return null;
@@ -2646,6 +2645,20 @@ protected byte[] _decodeBase64(Base64Variant b64variant) throws IOException
26462645
}
26472646
}
26482647

2648+
/*
2649+
/**********************************************************
2650+
/* Internal methods, location updating (refactored in 2.7)
2651+
/**********************************************************
2652+
*/
2653+
2654+
// @since 2.7
2655+
private final void _updateLocationFromInputPtr()
2656+
{
2657+
_tokenInputTotal = _currInputProcessed + _inputPtr - 1;
2658+
_tokenInputRow = _currInputRow;
2659+
_tokenInputCol = _inputPtr - _currInputRowStart - 1;
2660+
}
2661+
26492662
/*
26502663
/**********************************************************
26512664
/* Error reporting

src/main/java/com/fasterxml/jackson/core/json/UTF8StreamJsonParser.java

+27-19
Original file line numberDiff line numberDiff line change
@@ -697,25 +697,20 @@ public JsonToken nextToken() throws IOException
697697
close();
698698
return (_currToken = null);
699699
}
700-
701-
// First, need to ensure we know the starting location of token
702-
// after skipping leading white space
703-
_tokenInputTotal = _currInputProcessed + _inputPtr - 1;
704-
_tokenInputRow = _currInputRow;
705-
_tokenInputCol = _inputPtr - _currInputRowStart - 1;
706-
707-
// finally: clear any data retained so far
700+
// clear any data retained so far
708701
_binaryValue = null;
709702

710703
// Closing scope?
711704
if (i == INT_RBRACKET) {
705+
_updateLocationFromInputPtr();
712706
if (!_parsingContext.inArray()) {
713707
_reportMismatchedEndMarker(i, '}');
714708
}
715709
_parsingContext = _parsingContext.getParent();
716710
return (_currToken = JsonToken.END_ARRAY);
717711
}
718712
if (i == INT_RCURLY) {
713+
_updateLocationFromInputPtr();
719714
if (!_parsingContext.inObject()) {
720715
_reportMismatchedEndMarker(i, ']');
721716
}
@@ -730,6 +725,7 @@ public JsonToken nextToken() throws IOException
730725
}
731726
i = _skipWS();
732727
}
728+
_updateLocationFromInputPtr();
733729

734730
/* And should we now have a name? Always true for
735731
* Object contexts, since the intermediate 'expect-value'
@@ -848,6 +844,9 @@ private final JsonToken _nextAfterName()
848844
_nameCopied = false; // need to invalidate if it was copied
849845
JsonToken t = _nextToken;
850846
_nextToken = null;
847+
848+
// !!! 16-Nov-2015, tatu: TODO: fix [databind#37], copy next location to current here
849+
851850
// Also: may need to start new context?
852851
if (t == JsonToken.START_ARRAY) {
853852
_parsingContext = _parsingContext.createChildArrayContext(_tokenInputRow, _tokenInputCol);
@@ -867,7 +866,6 @@ private final JsonToken _nextAfterName()
867866
public boolean nextFieldName(SerializableString str) throws IOException
868867
{
869868
// // // Note: most of code below is copied from nextToken()
870-
871869
_numTypesValid = NR_UNKNOWN;
872870
if (_currToken == JsonToken.FIELD_NAME) { // can't have name right after name
873871
_nextAfterName();
@@ -882,15 +880,11 @@ public boolean nextFieldName(SerializableString str) throws IOException
882880
_currToken = null;
883881
return false;
884882
}
885-
_tokenInputTotal = _currInputProcessed + _inputPtr - 1;
886-
_tokenInputRow = _currInputRow;
887-
_tokenInputCol = _inputPtr - _currInputRowStart - 1;
888-
889-
// finally: clear any data retained so far
890883
_binaryValue = null;
891884

892885
// Closing scope?
893886
if (i == INT_RBRACKET) {
887+
_updateLocationFromInputPtr();
894888
if (!_parsingContext.inArray()) {
895889
_reportMismatchedEndMarker(i, '}');
896890
}
@@ -899,6 +893,7 @@ public boolean nextFieldName(SerializableString str) throws IOException
899893
return false;
900894
}
901895
if (i == INT_RCURLY) {
896+
_updateLocationFromInputPtr();
902897
if (!_parsingContext.inObject()) {
903898
_reportMismatchedEndMarker(i, ']');
904899
}
@@ -915,6 +910,7 @@ public boolean nextFieldName(SerializableString str) throws IOException
915910
i = _skipWS();
916911
}
917912

913+
_updateLocationFromInputPtr();
918914
if (!_parsingContext.inObject()) {
919915
_nextTokenNotInObject(i);
920916
return false;
@@ -971,13 +967,10 @@ public String nextFieldName() throws IOException
971967
_currToken = null;
972968
return null;
973969
}
974-
_tokenInputTotal = _currInputProcessed + _inputPtr - 1;
975-
_tokenInputRow = _currInputRow;
976-
_tokenInputCol = _inputPtr - _currInputRowStart - 1;
977-
978970
_binaryValue = null;
979971

980972
if (i == INT_RBRACKET) {
973+
_updateLocationFromInputPtr();
981974
if (!_parsingContext.inArray()) {
982975
_reportMismatchedEndMarker(i, '}');
983976
}
@@ -986,6 +979,7 @@ public String nextFieldName() throws IOException
986979
return null;
987980
}
988981
if (i == INT_RCURLY) {
982+
_updateLocationFromInputPtr();
989983
if (!_parsingContext.inObject()) {
990984
_reportMismatchedEndMarker(i, ']');
991985
}
@@ -1001,7 +995,7 @@ public String nextFieldName() throws IOException
1001995
}
1002996
i = _skipWS();
1003997
}
1004-
998+
_updateLocationFromInputPtr();
1005999
if (!_parsingContext.inObject()) {
10061000
_nextTokenNotInObject(i);
10071001
return null;
@@ -3608,6 +3602,20 @@ protected final byte[] _decodeBase64(Base64Variant b64variant) throws IOExceptio
36083602
}
36093603
}
36103604

3605+
/*
3606+
/**********************************************************
3607+
/* Internal methods, location updating (refactored in 2.7)
3608+
/**********************************************************
3609+
*/
3610+
3611+
// @since 2.7
3612+
private final void _updateLocationFromInputPtr()
3613+
{
3614+
_tokenInputTotal = _currInputProcessed + _inputPtr - 1;
3615+
_tokenInputRow = _currInputRow;
3616+
_tokenInputCol = _inputPtr - _currInputRowStart - 1;
3617+
}
3618+
36113619
/*
36123620
/**********************************************************
36133621
/* Internal methods, other

src/test/java/com/fasterxml/jackson/failing/LocationInArrayTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ private void _testOffsetInArrays(boolean useBytes) throws Exception
3939
_assertLocation(useBytes, p.getTokenLocation(), 7L, 1, 8);
4040
assertEquals(251, p.getIntValue()); // just to ensure read proceeds to end
4141
_assertLocation(useBytes, p.getCurrentLocation(), 10L, 1, 11);
42+
43+
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
44+
_assertLocation(useBytes, p.getTokenLocation(), 15L, 2, 4);
45+
assertEquals(3, p.getIntValue());
46+
_assertLocation(useBytes, p.getCurrentLocation(), 16L, 2, 5);
47+
48+
assertToken(JsonToken.END_ARRAY, p.nextToken());
49+
_assertLocation(useBytes, p.getTokenLocation(), 18L, 2, 7);
50+
_assertLocation(useBytes, p.getCurrentLocation(), 19L, 2, 8);
4251

4352
p.close();
4453
}

0 commit comments

Comments
 (0)