@@ -31,6 +31,8 @@ public class JSONTokener {
3131 private boolean usePrevious ;
3232 /** the number of characters read in the previous line. */
3333 private long characterPreviousLine ;
34+ /** number of non-whitespace characters read from the source. */
35+ private long contentCharCount ;
3436
3537 // access to this object is required for strict mode checking
3638 private JSONParserConfiguration jsonParserConfiguration ;
@@ -60,6 +62,7 @@ public JSONTokener(Reader reader, JSONParserConfiguration jsonParserConfiguratio
6062 this .usePrevious = false ;
6163 this .previous = 0 ;
6264 this .index = 0 ;
65+ this .contentCharCount = 0 ;
6366 this .character = 1 ;
6467 this .characterPreviousLine = 0 ;
6568 this .line = 1 ;
@@ -121,12 +124,14 @@ public void setJsonParserConfiguration(JSONParserConfiguration jsonParserConfigu
121124 }
122125
123126 /**
124- * Returns whether the tokener is currently positioned at the beginning,
127+ * Returns whether the tokener is positioned at the beginning,
128+ * i.e. only whitespace characters (or no characters at all) have been read so far.
129+ * Consuming and backing up over the first characters does not change the result.
125130 *
126- * @return true if the current input position is the beginning
131+ * @return true if no non-whitespace character has been read
127132 */
128- public boolean isAtStart () {
129- return this .index == 0 ;
133+ protected boolean isAtStart () {
134+ return this .contentCharCount == 0 ;
130135 }
131136
132137 /**
@@ -140,6 +145,9 @@ public void back() throws JSONException {
140145 if (this .usePrevious || this .index <= 0 ) {
141146 throw new JSONException ("Stepping back two steps is not supported" );
142147 }
148+ if (this .previous > ' ' ) {
149+ this .contentCharCount --;
150+ }
143151 this .decrementIndexes ();
144152 this .usePrevious = true ;
145153 this .eof = false ;
@@ -240,6 +248,9 @@ public char next() throws JSONException {
240248 return 0 ;
241249 }
242250 this .incrementIndexes (c );
251+ if (c > ' ' ) {
252+ this .contentCharCount ++;
253+ }
243254 this .previous = (char ) c ;
244255 return this .previous ;
245256 }
@@ -558,6 +569,7 @@ public char skipTo(char to) throws JSONException {
558569 long startIndex = this .index ;
559570 long startCharacter = this .character ;
560571 long startLine = this .line ;
572+ long startContentCharCount = this .contentCharCount ;
561573 this .reader .mark (1000000 );
562574 do {
563575 c = this .next ();
@@ -569,6 +581,7 @@ public char skipTo(char to) throws JSONException {
569581 this .index = startIndex ;
570582 this .character = startCharacter ;
571583 this .line = startLine ;
584+ this .contentCharCount = startContentCharCount ;
572585 return 0 ;
573586 }
574587 } while (c != to );
0 commit comments