1 package org
.codehaus
.groovy
.grails
.web
.json
;
4 import java
.util
.regex
.Matcher
;
5 import java
.util
.regex
.Pattern
;
8 Copyright (c) 2002 JSON.org
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
20 The Software shall be used for Good, not Evil.
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * A JSONTokener takes a source string and extracts characters and tokens from
33 * it. It is used by the JSONObject and JSONArray constructors to parse
34 * JSON source strings.
39 public class JSONTokener
{
42 * The index of the next character.
48 * The source string being tokenized.
50 private String mySource
;
54 * Construct a JSONTokener from a string.
56 * @param s A source string.
58 public JSONTokener(String s
) {
65 * Back up one character. This provides a sort of lookahead capability,
66 * so that you can test for a digit or letter before attempting to parse
67 * the next number or identifier.
70 if (this.myIndex
> 0) {
77 * Get the hex value of a character (base16).
79 * @param c A character between '0' and '9' or between 'A' and 'F' or
80 * between 'a' and 'f'.
81 * @return An int between 0 and 15, or -1 if c was not a hex digit.
83 public static int dehexchar(char c
) {
84 if (c
>= '0' && c
<= '9') {
87 if (c
>= 'A' && c
<= 'F') {
88 return c
- ('A' - 10);
90 if (c
>= 'a' && c
<= 'f') {
91 return c
- ('a' - 10);
98 * Determine if the source string still contains characters that next()
101 * @return true if not yet at the end of the source.
103 public boolean more() {
104 return this.myIndex
< this.mySource
.length();
109 * Get the next character in the source string.
111 * @return The next character, or 0 if past the end of the source string.
115 char c
= this.mySource
.charAt(this.myIndex
);
124 * Consume the next character, and check that it matches a specified
127 * @param c The character to match.
128 * @return The character.
129 * @throws JSONException if the character does not match.
131 public char next(char c
) throws JSONException
{
134 throw syntaxError("Expected '" + c
+ "' and instead saw '" +
142 * Get the next n characters.
144 * @param n The number of characters to take.
145 * @return A string of n characters.
146 * @throws JSONException Substring bounds error if there are not
147 * n characters remaining in the source string.
149 public String
next(int n
) throws JSONException
{
150 int i
= this.myIndex
;
152 if (j
>= this.mySource
.length()) {
153 throw syntaxError("Substring bounds error");
156 return this.mySource
.substring(i
, j
);
161 * Get the next char in the string, skipping whitespace
162 * and comments (slashslash, slashstar, and hash).
164 * @return A character, or 0 if there are no more characters.
165 * @throws JSONException
167 public char nextClean() throws JSONException
{
175 } while (c
!= '\n' && c
!= '\r' && c
!= 0);
181 throw syntaxError("Unclosed comment.");
195 } else if (c
== '#') {
198 } while (c
!= '\n' && c
!= '\r' && c
!= 0);
199 } else if (c
== 0 || c
> ' ') {
207 * Return the characters up to the next close quote character.
208 * Backslash processing is done. The formal JSON format does not
209 * allow strings in single quotes, but an implementation is allowed to
212 * @param quote The quoting character, either
213 * <code>"</code> <small>(double quote)</small> or
214 * <code>'</code> <small>(single quote)</small>.
216 * @throws JSONException Unterminated string.
218 public String
nextString(char quote
) throws JSONException
{
220 StringBuffer sb
= new StringBuffer();
227 throw syntaxError("Unterminated string");
247 sb
.append((char) Integer
.parseInt(next(4), 16));
250 sb
.append((char) Integer
.parseInt(next(2), 16));
258 return sb
.toString();
267 * Get the text up but not including the specified character or the
268 * end of line, whichever comes first.
270 * @param d A delimiter character.
273 public String
nextTo(char d
) {
274 StringBuffer sb
= new StringBuffer();
277 if (c
== d
|| c
== 0 || c
== '\n' || c
== '\r') {
281 return sb
.toString().trim();
289 * Get the text up but not including one of the specified delimeter
290 * characters or the end of line, whichever comes first.
292 * @param delimiters A set of delimiter characters.
293 * @return A string, trimmed.
295 public String
nextTo(String delimiters
) {
297 StringBuffer sb
= new StringBuffer();
300 if (delimiters
.indexOf(c
) >= 0 || c
== 0 ||
301 c
== '\n' || c
== '\r') {
305 return sb
.toString().trim();
313 * Get the next value. The value can be a Boolean, Double, Integer,
314 * JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object.
317 * @throws JSONException If syntax error.
319 public Object
nextValue() throws JSONException
{
320 char c
= nextClean();
326 return nextString(c
);
329 return new JSONObject(this);
332 return new JSONArray(this);
336 * Handle unquoted text. This could be the values true, false, or
337 * null, or it can be a number. An implementation (such as this one)
338 * is allowed to also accept non-standard forms.
340 * Accumulate characters until we reach the end of the text or a
341 * formatting character.
344 StringBuffer sb
= new StringBuffer();
346 while (c
>= ' ' && ",:]}/\\\"[{;=#".indexOf(c
) < 0) {
353 * If it is true, false, or null, return the proper value.
356 s
= sb
.toString().trim();
358 throw syntaxError("Missing value.");
360 if (s
.equalsIgnoreCase("true")) {
363 if (s
.equalsIgnoreCase("false")) {
364 return Boolean
.FALSE
;
366 if (s
.equalsIgnoreCase("null")) {
367 return JSONObject
.NULL
;
370 if (s
.startsWith("new Date(")) {
372 Matcher matcher
= Pattern
.compile("^\\s*new\\s+Date\\(\\s*(\\d+)\\s*\\)\\s*$").matcher(s
);
373 if (matcher
.find()) {
374 long time
= Long
.parseLong(matcher
.group(1));
375 return new Date(time
);
377 } catch (Exception e
) {
383 * If it might be a number, try converting it. We support the 0- and 0x-
384 * conventions. If a number cannot be produced, then the value will just
385 * be a string. Note that the 0-, 0x-, plus, and implied string
386 * conventions are non-standard. A JSON parser is free to accept
387 * non-JSON forms as long as it accepts all correct JSON forms.
390 if ((b
>= '0' && b
<= '9') || b
== '.' || b
== '-' || b
== '+') {
392 if (s
.length() > 2 &&
393 (s
.charAt(1) == 'x' || s
.charAt(1) == 'X')) {
395 return new Integer(Integer
.parseInt(s
.substring(2),
397 } catch (Exception e
) {
398 /* Ignore the error */
402 return new Integer(Integer
.parseInt(s
, 8));
403 } catch (Exception e
) {
404 /* Ignore the error */
409 return new Integer(s
);
410 } catch (Exception e
) {
413 } catch (Exception f
) {
415 return new Double(s
);
416 } catch (Exception g
) {
427 * Skip characters until the next character is the requested character.
428 * If the requested character is not found, no characters are skipped.
430 * @param to A character to skip to.
431 * @return The requested character, or zero if the requested character
434 public char skipTo(char to
) {
436 int index
= this.myIndex
;
440 this.myIndex
= index
;
450 * Skip characters until past the requested string.
451 * If it is not found, we are left at the end of the source.
453 * @param to A string to skip past.
455 public void skipPast(String to
) {
456 this.myIndex
= this.mySource
.indexOf(to
, this.myIndex
);
457 if (this.myIndex
< 0) {
458 this.myIndex
= this.mySource
.length();
460 this.myIndex
+= to
.length();
466 * Make a JSONException to signal a syntax error.
468 * @param message The error message.
469 * @return A JSONException object, suitable for throwing
471 public JSONException
syntaxError(String message
) {
472 return new JSONException(message
+ toString());
477 * Make a printable string of this JSONTokener.
479 * @return " at character [this.myIndex] of [this.mySource]"
481 public String
toString() {
482 return " at character " + this.myIndex
+ " of " + this.mySource
;