Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package org.json

File JSONTokener.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
56% of files have more coverage

Code metrics

60
190
21
1
529
301
87
0.46
9.05
21
4.14

Classes

Class Line # Actions
JSONTokener 41 190 87
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package org.json;
2   
3    import java.io.BufferedReader;
4    import java.io.IOException;
5    import java.io.InputStream;
6    import java.io.InputStreamReader;
7    import java.io.Reader;
8    import java.io.StringReader;
9   
10    /*
11    Copyright (c) 2002 JSON.org
12   
13    Permission is hereby granted, free of charge, to any person obtaining a copy
14    of this software and associated documentation files (the "Software"), to deal
15    in the Software without restriction, including without limitation the rights
16    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17    copies of the Software, and to permit persons to whom the Software is
18    furnished to do so, subject to the following conditions:
19   
20    The above copyright notice and this permission notice shall be included in all
21    copies or substantial portions of the Software.
22   
23    The Software shall be used for Good, not Evil.
24   
25    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31    SOFTWARE.
32    */
33   
34    /**
35    * A JSONTokener takes a source string and extracts characters and tokens from
36    * it. It is used by the JSONObject and JSONArray constructors to parse
37    * JSON source strings.
38    * @author JSON.org
39    * @version 2014-05-03
40    */
 
41    public class JSONTokener {
42    /** current read character position on the current line. */
43    private long character;
44    /** flag to indicate if the end of the input has been found. */
45    private boolean eof;
46    /** current read index of the input. */
47    private long index;
48    /** current line of the input. */
49    private long line;
50    /** previous character read from the input. */
51    private char previous;
52    /** Reader for the input. */
53    private final Reader reader;
54    /** flag to indicate that a previous character was requested. */
55    private boolean usePrevious;
56    /** the number of characters read in the previous line. */
57    private long characterPreviousLine;
58   
59   
60    /**
61    * Construct a JSONTokener from a Reader. The caller must close the Reader.
62    *
63    * @param reader A reader.
64    */
 
65  0 toggle public JSONTokener(Reader reader) {
66  0 this.reader = reader.markSupported()
67    ? reader
68    : new BufferedReader(reader);
69  0 this.eof = false;
70  0 this.usePrevious = false;
71  0 this.previous = 0;
72  0 this.index = 0;
73  0 this.character = 1;
74  0 this.characterPreviousLine = 0;
75  0 this.line = 1;
76    }
77   
78   
79    /**
80    * Construct a JSONTokener from an InputStream. The caller must close the input stream.
81    * @param inputStream The source.
82    */
 
83  0 toggle public JSONTokener(InputStream inputStream) {
84  0 this(new InputStreamReader(inputStream));
85    }
86   
87   
88    /**
89    * Construct a JSONTokener from a string.
90    *
91    * @param s A source string.
92    */
 
93  0 toggle public JSONTokener(String s) {
94  0 this(new StringReader(s));
95    }
96   
97   
98    /**
99    * Back up one character. This provides a sort of lookahead capability,
100    * so that you can test for a digit or letter before attempting to parse
101    * the next number or identifier.
102    * @throws JSONException Thrown if trying to step back more than 1 step
103    * or if already at the start of the string
104    */
 
105  0 toggle public void back() throws JSONException {
106  0 if (this.usePrevious || this.index <= 0) {
107  0 throw new JSONException("Stepping back two steps is not supported");
108    }
109  0 this.decrementIndexes();
110  0 this.usePrevious = true;
111  0 this.eof = false;
112    }
113   
114    /**
115    * Decrements the indexes for the {@link #back()} method based on the previous character read.
116    */
 
117  0 toggle private void decrementIndexes() {
118  0 this.index--;
119  0 if(this.previous=='\r' || this.previous == '\n') {
120  0 this.line--;
121  0 this.character=this.characterPreviousLine ;
122  0 } else if(this.character > 0){
123  0 this.character--;
124    }
125    }
126   
127    /**
128    * Get the hex value of a character (base16).
129    * @param c A character between '0' and '9' or between 'A' and 'F' or
130    * between 'a' and 'f'.
131    * @return An int between 0 and 15, or -1 if c was not a hex digit.
132    */
 
133  0 toggle public static int dehexchar(char c) {
134  0 if (c >= '0' && c <= '9') {
135  0 return c - '0';
136    }
137  0 if (c >= 'A' && c <= 'F') {
138  0 return c - ('A' - 10);
139    }
140  0 if (c >= 'a' && c <= 'f') {
141  0 return c - ('a' - 10);
142    }
143  0 return -1;
144    }
145   
146    /**
147    * Checks if the end of the input has been reached.
148    *
149    * @return true if at the end of the file and we didn't step back
150    */
 
151  0 toggle public boolean end() {
152  0 return this.eof && !this.usePrevious;
153    }
154   
155   
156    /**
157    * Determine if the source string still contains characters that next()
158    * can consume.
159    * @return true if not yet at the end of the source.
160    * @throws JSONException thrown if there is an error stepping forward
161    * or backward while checking for more data.
162    */
 
163  0 toggle public boolean more() throws JSONException {
164  0 if(this.usePrevious) {
165  0 return true;
166    }
167  0 try {
168  0 this.reader.mark(1);
169    } catch (IOException e) {
170  0 throw new JSONException("Unable to preserve stream position", e);
171    }
172  0 try {
173    // -1 is EOF, but next() can not consume the null character '\0'
174  0 if(this.reader.read() <= 0) {
175  0 this.eof = true;
176  0 return false;
177    }
178  0 this.reader.reset();
179    } catch (IOException e) {
180  0 throw new JSONException("Unable to read the next character from the stream", e);
181    }
182  0 return true;
183    }
184   
185   
186    /**
187    * Get the next character in the source string.
188    *
189    * @return The next character, or 0 if past the end of the source string.
190    * @throws JSONException Thrown if there is an error reading the source string.
191    */
 
192  0 toggle public char next() throws JSONException {
193  0 int c;
194  0 if (this.usePrevious) {
195  0 this.usePrevious = false;
196  0 c = this.previous;
197    } else {
198  0 try {
199  0 c = this.reader.read();
200    } catch (IOException exception) {
201  0 throw new JSONException(exception);
202    }
203    }
204  0 if (c <= 0) { // End of stream
205  0 this.eof = true;
206  0 return 0;
207    }
208  0 this.incrementIndexes(c);
209  0 this.previous = (char) c;
210  0 return this.previous;
211    }
212   
213    /**
214    * Increments the internal indexes according to the previous character
215    * read and the character passed as the current character.
216    * @param c the current character read.
217    */
 
218  0 toggle private void incrementIndexes(int c) {
219  0 if(c > 0) {
220  0 this.index++;
221  0 if(c=='\r') {
222  0 this.line++;
223  0 this.characterPreviousLine = this.character;
224  0 this.character=0;
225  0 }else if (c=='\n') {
226  0 if(this.previous != '\r') {
227  0 this.line++;
228  0 this.characterPreviousLine = this.character;
229    }
230  0 this.character=0;
231    } else {
232  0 this.character++;
233    }
234    }
235    }
236   
237    /**
238    * Consume the next character, and check that it matches a specified
239    * character.
240    * @param c The character to match.
241    * @return The character.
242    * @throws JSONException if the character does not match.
243    */
 
244  0 toggle public char next(char c) throws JSONException {
245  0 char n = this.next();
246  0 if (n != c) {
247  0 if(n > 0) {
248  0 throw this.syntaxError("Expected '" + c + "' and instead saw '" +
249    n + "'");
250    }
251  0 throw this.syntaxError("Expected '" + c + "' and instead saw ''");
252    }
253  0 return n;
254    }
255   
256   
257    /**
258    * Get the next n characters.
259    *
260    * @param n The number of characters to take.
261    * @return A string of n characters.
262    * @throws JSONException
263    * Substring bounds error if there are not
264    * n characters remaining in the source string.
265    */
 
266  0 toggle public String next(int n) throws JSONException {
267  0 if (n == 0) {
268  0 return "";
269    }
270   
271  0 char[] chars = new char[n];
272  0 int pos = 0;
273   
274  0 while (pos < n) {
275  0 chars[pos] = this.next();
276  0 if (this.end()) {
277  0 throw this.syntaxError("Substring bounds error");
278    }
279  0 pos += 1;
280    }
281  0 return new String(chars);
282    }
283   
284   
285    /**
286    * Get the next char in the string, skipping whitespace.
287    * @throws JSONException Thrown if there is an error reading the source string.
288    * @return A character, or 0 if there are no more characters.
289    */
 
290  0 toggle public char nextClean() throws JSONException {
291  0 for (;;) {
292  0 char c = this.next();
293  0 if (c == 0 || c > ' ') {
294  0 return c;
295    }
296    }
297    }
298   
299   
300    /**
301    * Return the characters up to the next close quote character.
302    * Backslash processing is done. The formal JSON format does not
303    * allow strings in single quotes, but an implementation is allowed to
304    * accept them.
305    * @param quote The quoting character, either
306    * <code>"</code>&nbsp;<small>(double quote)</small> or
307    * <code>'</code>&nbsp;<small>(single quote)</small>.
308    * @return A String.
309    * @throws JSONException Unterminated string.
310    */
 
311  0 toggle public String nextString(char quote) throws JSONException {
312  0 char c;
313  0 StringBuilder sb = new StringBuilder();
314  0 for (;;) {
315  0 c = this.next();
316  0 switch (c) {
317  0 case 0:
318  0 case '\n':
319  0 case '\r':
320  0 throw this.syntaxError("Unterminated string");
321  0 case '\\':
322  0 c = this.next();
323  0 switch (c) {
324  0 case 'b':
325  0 sb.append('\b');
326  0 break;
327  0 case 't':
328  0 sb.append('\t');
329  0 break;
330  0 case 'n':
331  0 sb.append('\n');
332  0 break;
333  0 case 'f':
334  0 sb.append('\f');
335  0 break;
336  0 case 'r':
337  0 sb.append('\r');
338  0 break;
339  0 case 'u':
340  0 try {
341  0 sb.append((char)Integer.parseInt(this.next(4), 16));
342    } catch (NumberFormatException e) {
343  0 throw this.syntaxError("Illegal escape.", e);
344    }
345  0 break;
346  0 case '"':
347  0 case '\'':
348  0 case '\\':
349  0 case '/':
350  0 sb.append(c);
351  0 break;
352  0 default:
353  0 throw this.syntaxError("Illegal escape.");
354    }
355  0 break;
356  0 default:
357  0 if (c == quote) {
358  0 return sb.toString();
359    }
360  0 sb.append(c);
361    }
362    }
363    }
364   
365   
366    /**
367    * Get the text up but not including the specified character or the
368    * end of line, whichever comes first.
369    * @param delimiter A delimiter character.
370    * @return A string.
371    * @throws JSONException Thrown if there is an error while searching
372    * for the delimiter
373    */
 
374  0 toggle public String nextTo(char delimiter) throws JSONException {
375  0 StringBuilder sb = new StringBuilder();
376  0 for (;;) {
377  0 char c = this.next();
378  0 if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
379  0 if (c != 0) {
380  0 this.back();
381    }
382  0 return sb.toString().trim();
383    }
384  0 sb.append(c);
385    }
386    }
387   
388   
389    /**
390    * Get the text up but not including one of the specified delimiter
391    * characters or the end of line, whichever comes first.
392    * @param delimiters A set of delimiter characters.
393    * @return A string, trimmed.
394    * @throws JSONException Thrown if there is an error while searching
395    * for the delimiter
396    */
 
397  0 toggle public String nextTo(String delimiters) throws JSONException {
398  0 char c;
399  0 StringBuilder sb = new StringBuilder();
400  0 for (;;) {
401  0 c = this.next();
402  0 if (delimiters.indexOf(c) >= 0 || c == 0 ||
403    c == '\n' || c == '\r') {
404  0 if (c != 0) {
405  0 this.back();
406    }
407  0 return sb.toString().trim();
408    }
409  0 sb.append(c);
410    }
411    }
412   
413   
414    /**
415    * Get the next value. The value can be a Boolean, Double, Integer,
416    * JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object.
417    * @throws JSONException If syntax error.
418    *
419    * @return An object.
420    */
 
421  0 toggle public Object nextValue() throws JSONException {
422  0 char c = this.nextClean();
423  0 String string;
424   
425  0 switch (c) {
426  0 case '"':
427  0 case '\'':
428  0 return this.nextString(c);
429  0 case '{':
430  0 this.back();
431  0 return new JSONObject(this);
432  0 case '[':
433  0 this.back();
434  0 return new JSONArray(this);
435    }
436   
437    /*
438    * Handle unquoted text. This could be the values true, false, or
439    * null, or it can be a number. An implementation (such as this one)
440    * is allowed to also accept non-standard forms.
441    *
442    * Accumulate characters until we reach the end of the text or a
443    * formatting character.
444    */
445   
446  0 StringBuilder sb = new StringBuilder();
447  0 while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
448  0 sb.append(c);
449  0 c = this.next();
450    }
451  0 this.back();
452   
453  0 string = sb.toString().trim();
454  0 if ("".equals(string)) {
455  0 throw this.syntaxError("Missing value");
456    }
457  0 return JSONObject.stringToValue(string);
458    }
459   
460   
461    /**
462    * Skip characters until the next character is the requested character.
463    * If the requested character is not found, no characters are skipped.
464    * @param to A character to skip to.
465    * @return The requested character, or zero if the requested character
466    * is not found.
467    * @throws JSONException Thrown if there is an error while searching
468    * for the to character
469    */
 
470  0 toggle public char skipTo(char to) throws JSONException {
471  0 char c;
472  0 try {
473  0 long startIndex = this.index;
474  0 long startCharacter = this.character;
475  0 long startLine = this.line;
476  0 this.reader.mark(1000000);
477  0 do {
478  0 c = this.next();
479  0 if (c == 0) {
480    // in some readers, reset() may throw an exception if
481    // the remaining portion of the input is greater than
482    // the mark size (1,000,000 above).
483  0 this.reader.reset();
484  0 this.index = startIndex;
485  0 this.character = startCharacter;
486  0 this.line = startLine;
487  0 return 0;
488    }
489  0 } while (c != to);
490  0 this.reader.mark(1);
491    } catch (IOException exception) {
492  0 throw new JSONException(exception);
493    }
494  0 this.back();
495  0 return c;
496    }
497   
498    /**
499    * Make a JSONException to signal a syntax error.
500    *
501    * @param message The error message.
502    * @return A JSONException object, suitable for throwing
503    */
 
504  0 toggle public JSONException syntaxError(String message) {
505  0 return new JSONException(message + this.toString());
506    }
507   
508    /**
509    * Make a JSONException to signal a syntax error.
510    *
511    * @param message The error message.
512    * @param causedBy The throwable that caused the error.
513    * @return A JSONException object, suitable for throwing
514    */
 
515  0 toggle public JSONException syntaxError(String message, Throwable causedBy) {
516  0 return new JSONException(message + this.toString(), causedBy);
517    }
518   
519    /**
520    * Make a printable string of this JSONTokener.
521    *
522    * @return " at {index} [character {character} line {line}]"
523    */
 
524  0 toggle @Override
525    public String toString() {
526  0 return " at " + this.index + " [character " + this.character + " line " +
527    this.line + "]";
528    }
529    }