1 package org
.gaixie
.jibu
.json
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.IOException
;
6 import java
.io
.StringReader
;
9 Copyright (c) 2002 JSON.org
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
21 The Software shall be used for Good, not Evil.
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * A JSONTokener takes a source string and extracts characters and tokens from
34 * it. It is used by the JSONObject and JSONArray constructors to parse
35 * JSON source strings.
39 public class JSONTokener
{
42 private Reader reader
;
43 private char lastChar
;
44 private boolean useLastChar
;
48 * Construct a JSONTokener from a string.
50 * @param reader A reader.
52 public JSONTokener(Reader reader
) {
53 this.reader
= reader
.markSupported() ?
54 reader
: new BufferedReader(reader
);
55 this.useLastChar
= false;
61 * Construct a JSONTokener from a string.
63 * @param s A source string.
65 public JSONTokener(String s
) {
66 this(new StringReader(s
));
71 * Back up one character. This provides a sort of lookahead capability,
72 * so that you can test for a digit or letter before attempting to parse
73 * the next number or identifier.
75 public void back() throws JSONException
{
76 if (useLastChar
|| index
<= 0) {
77 throw new JSONException("Stepping back two steps is not supported");
86 * Get the hex value of a character (base16).
87 * @param c A character between '0' and '9' or between 'A' and 'F' or
88 * between 'a' and 'f'.
89 * @return An int between 0 and 15, or -1 if c was not a hex digit.
91 public static int dehexchar(char c
) {
92 if (c
>= '0' && c
<= '9') {
95 if (c
>= 'A' && c
<= 'F') {
96 return c
- ('A' - 10);
98 if (c
>= 'a' && c
<= 'f') {
99 return c
- ('a' - 10);
106 * Determine if the source string still contains characters that next()
108 * @return true if not yet at the end of the source.
110 public boolean more() throws JSONException
{
111 char nextChar
= next();
121 * Get the next character in the source string.
123 * @return The next character, or 0 if past the end of the source string.
125 public char next() throws JSONException
{
126 if (this.useLastChar
) {
127 this.useLastChar
= false;
128 if (this.lastChar
!= 0) {
131 return this.lastChar
;
135 c
= this.reader
.read();
136 } catch (IOException exc
) {
137 throw new JSONException(exc
);
140 if (c
<= 0) { // End of stream
145 this.lastChar
= (char) c
;
146 return this.lastChar
;
151 * Consume the next character, and check that it matches a specified
153 * @param c The character to match.
154 * @return The character.
155 * @throws JSONException if the character does not match.
157 public char next(char c
) throws JSONException
{
160 throw syntaxError("Expected '" + c
+ "' and instead saw '" +
168 * Get the next n characters.
170 * @param n The number of characters to take.
171 * @return A string of n characters.
172 * @throws JSONException
173 * Substring bounds error if there are not
174 * n characters remaining in the source string.
176 public String
next(int n
) throws JSONException
{
181 char[] buffer
= new char[n
];
184 if (this.useLastChar
) {
185 this.useLastChar
= false;
186 buffer
[0] = this.lastChar
;
192 while ((pos
< n
) && ((len
= reader
.read(buffer
, pos
, n
- pos
)) != -1)) {
195 } catch (IOException exc
) {
196 throw new JSONException(exc
);
201 throw syntaxError("Substring bounds error");
204 this.lastChar
= buffer
[n
- 1];
205 return new String(buffer
);
210 * Get the next char in the string, skipping whitespace.
211 * @throws JSONException
212 * @return A character, or 0 if there are no more characters.
214 public char nextClean() throws JSONException
{
217 if (c
== 0 || c
> ' ') {
225 * Return the characters up to the next close quote character.
226 * Backslash processing is done. The formal JSON format does not
227 * allow strings in single quotes, but an implementation is allowed to
229 * @param quote The quoting character, either
230 * <code>"</code> <small>(double quote)</small> or
231 * <code>'</code> <small>(single quote)</small>.
233 * @throws JSONException Unterminated string.
235 public String
nextString(char quote
) throws JSONException
{
237 StringBuffer sb
= new StringBuffer();
244 throw syntaxError("Unterminated string");
264 sb
.append((char)Integer
.parseInt(next(4), 16));
273 throw syntaxError("Illegal escape.");
278 return sb
.toString();
287 * Get the text up but not including the specified character or the
288 * end of line, whichever comes first.
289 * @param d A delimiter character.
292 public String
nextTo(char d
) throws JSONException
{
293 StringBuffer sb
= new StringBuffer();
296 if (c
== d
|| c
== 0 || c
== '\n' || c
== '\r') {
300 return sb
.toString().trim();
308 * Get the text up but not including one of the specified delimiter
309 * characters or the end of line, whichever comes first.
310 * @param delimiters A set of delimiter characters.
311 * @return A string, trimmed.
313 public String
nextTo(String delimiters
) throws JSONException
{
315 StringBuffer sb
= new StringBuffer();
318 if (delimiters
.indexOf(c
) >= 0 || c
== 0 ||
319 c
== '\n' || c
== '\r') {
323 return sb
.toString().trim();
331 * Get the next value. The value can be a Boolean, Double, Integer,
332 * JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object.
333 * @throws JSONException If syntax error.
337 public Object
nextValue() throws JSONException
{
338 char c
= nextClean();
344 return nextString(c
);
347 return new JSONObject(this);
351 return new JSONArray(this);
355 * Handle unquoted text. This could be the values true, false, or
356 * null, or it can be a number. An implementation (such as this one)
357 * is allowed to also accept non-standard forms.
359 * Accumulate characters until we reach the end of the text or a
360 * formatting character.
363 StringBuffer sb
= new StringBuffer();
364 while (c
>= ' ' && ",:]}/\\\"[{;=#".indexOf(c
) < 0) {
370 s
= sb
.toString().trim();
372 throw syntaxError("Missing value");
374 return JSONObject
.stringToValue(s
);
379 * Skip characters until the next character is the requested character.
380 * If the requested character is not found, no characters are skipped.
381 * @param to A character to skip to.
382 * @return The requested character, or zero if the requested character
385 public char skipTo(char to
) throws JSONException
{
388 int startIndex
= this.index
;
389 reader
.mark(Integer
.MAX_VALUE
);
394 this.index
= startIndex
;
398 } catch (IOException exc
) {
399 throw new JSONException(exc
);
407 * Make a JSONException to signal a syntax error.
409 * @param message The error message.
410 * @return A JSONException object, suitable for throwing
412 public JSONException
syntaxError(String message
) {
413 return new JSONException(message
+ toString());
418 * Make a printable string of this JSONTokener.
420 * @return " at character [this.index]"
422 public String
toString() {
423 return " at character " + index
;