2 Adobe Systems Incorporated(r) Source Code License Agreement
3 Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
5 Please read this Source Code License Agreement carefully before using
8 Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive,
9 no-charge, royalty-free, irrevocable copyright license, to reproduce,
10 prepare derivative works of, publicly display, publicly perform, and
11 distribute this source code and such derivative works in source or
12 object code form without any attribution requirements.
14 The name "Adobe Systems Incorporated" must not be used to endorse or promote products
15 derived from the source code without prior written permission.
17 You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
18 against any loss, damage, claims or lawsuits, including attorney's
19 fees that arise or result from your use or distribution of the source
22 THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT
23 ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
24 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF
26 NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA
27 OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
33 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 package com.adobe.serialization.json {
41 public class JSONTokenizer {
43 /** The object that will get parsed from the JSON string */
44 private var obj:Object;
46 /** The JSON string to be parsed */
47 private var jsonString:String;
49 /** The current parsing location in the JSON string */
52 /** The current character in the JSON string during parsing */
53 private var ch:String;
56 * Constructs a new JSONDecoder to parse a JSON string
57 * into a native object.
59 * @param s The JSON string to be converted
60 * into a native object
62 public function JSONTokenizer( s:String ) {
66 // prime the pump by getting the first character
71 * Gets the next token in the input sting and advances
72 * the character to the next character after the token
74 public function getNextToken():JSONToken {
75 var token:JSONToken = new JSONToken();
77 // skip any whitespace / comments since the last
81 // examine the new character and see what we have...
85 token.type = JSONTokenType.LEFT_BRACE;
91 token.type = JSONTokenType.RIGHT_BRACE;
97 token.type = JSONTokenType.LEFT_BRACKET;
103 token.type = JSONTokenType.RIGHT_BRACKET;
109 token.type = JSONTokenType.COMMA;
115 token.type = JSONTokenType.COLON;
120 case 't': // attempt to read true
121 var possibleTrue:String = "t" + nextChar() + nextChar() + nextChar();
123 if ( possibleTrue == "true" ) {
124 token.type = JSONTokenType.TRUE;
128 parseError( "Expecting 'true' but found " + possibleTrue );
133 case 'f': // attempt to read false
134 var possibleFalse:String = "f" + nextChar() + nextChar() + nextChar() + nextChar();
136 if ( possibleFalse == "false" ) {
137 token.type = JSONTokenType.FALSE;
141 parseError( "Expecting 'false' but found " + possibleFalse );
146 case 'n': // attempt to read null
148 var possibleNull:String = "n" + nextChar() + nextChar() + nextChar();
150 if ( possibleNull == "null" ) {
151 token.type = JSONTokenType.NULL;
155 parseError( "Expecting 'null' but found " + possibleNull );
160 case '"': // the start of a string
161 token = readString();
165 // see if we can read a number
166 if ( isDigit( ch ) || ch == '-' ) {
167 token = readNumber();
168 } else if ( ch == '' ) {
169 // check for reading past the end of the string
172 // not sure what was in the input string - it's not
173 // anything we expected
174 parseError( "Unexpected " + ch + " encountered" );
182 * Attempts to read a string from the input string. Places
183 * the character location at the first character after the
184 * string. It is assumed that ch is " before this method is called.
186 * @return the JSONToken with the string value if a string could
187 * be read. Throws an error otherwise.
189 private function readString():JSONToken {
190 // the token for the string we'll try to read
191 var token:JSONToken = new JSONToken();
192 token.type = JSONTokenType.STRING;
194 // the string to store the string we'll try to read
195 var string:String = "";
197 // advance past the first "
200 while ( ch != '"' && ch != '' ) {
202 // unescape the escape sequences in the string
205 // get the next character so we know what
211 case '"': // quotation mark
219 case '\\': // reverse solidus
227 case 'f': // form feed
235 case 'r': // carriage return
239 case 't': // horizontal tab
244 // convert a unicode escape sequence
245 // to it's character value - expecting
248 // save the characters as a string we'll convert to an int
249 var hexValue:String = "";
251 // try to find 4 hex characters
252 for ( var i:int = 0; i < 4; i++ ) {
253 // get the next character and determine
254 // if it's a valid hex digit or not
255 if ( !isHexDigit( nextChar() ) ) {
256 parseError( " Excepted a hex digit, but found: " + ch );
258 // valid, add it to the value
262 // convert hexValue to an integer, and use that
263 // integrer value to create a character to add
265 string += String.fromCharCode( parseInt( hexValue, 16 ) );
270 // couldn't unescape the sequence, so just
277 // didn't have to unescape, so add the character to the string
282 // move to the next character
287 // we read past the end of the string without closing it, which
290 parseError( "Unterminated string literal" );
293 // move past the closing " in the input string
296 // attach to the string to the token so we can return it
297 token.value = string;
303 * Attempts to read a number from the input string. Places
304 * the character location at the first character after the
307 * @return The JSONToken with the number value if a number could
308 * be read. Throws an error otherwise.
310 private function readNumber():JSONToken {
311 // the token for the number we'll try to read
312 var token:JSONToken = new JSONToken();
313 token.type = JSONTokenType.NUMBER;
315 // the string to accumulate the number characters
316 // into that we'll convert to a number at the end
317 var input:String = "";
319 // check for a negative number
325 // read numbers while we can
326 while ( isDigit( ch ) ) {
331 // check for a decimal value
335 // read more numbers to get the decimal value
336 while ( isDigit( ch ) ) {
342 //Application.application.show( "number = " + input );
344 // conver the string to a number value
345 var num:Number = Number( input );
347 if ( isFinite( num ) ) {
351 parseError( "Number " + num + " is not valid!" );
357 * Reads the next character in the input
358 * string and advances the character location.
360 * @return The next character in the input string, or
361 * null if we've read past the end.
363 private function nextChar():String {
364 return ch = jsonString.charAt( loc++ );
368 * Advances the character location past any
369 * sort of white space and comments
371 private function skipIgnored():void {
378 * Skips comments in the input string, either
379 * single-line or multi-line. Advances the character
380 * to the first position after the end of the comment.
382 private function skipComments():void {
384 // Advance past the first / to find out what type of comment
387 case '/': // single-line comment, read through end of line
389 // Loop over the characters until we find
390 // a newline or until there's no more characters left
393 } while ( ch != '\n' && ch != '' )
400 case '*': // multi-line comment, read until closing */
402 // move past the opening *
405 // try to find a trailing */
408 // check to see if we have a closing /
411 // move past the end of the closing */
416 // move along, looking if the next character is a *
420 // when we're here we've read past the end of
421 // the string without finding a closing */, so error
423 parseError( "Multi-line comment not closed" );
429 // Can't match a comment after a /, so it's a parsing error
431 parseError( "Unexpected " + ch + " encountered (expecting '/' or '*' )" );
439 * Skip any whitespace in the input string and advances
440 * the character to the first character after any possible
443 private function skipWhite():void {
445 // As long as there are spaces in the input
446 // stream, advance the current location pointer
448 while ( isSpace( ch ) ) {
455 * Determines if a character is whitespace or not.
457 * @return True if the character passed in is a whitespace
460 private function isSpace( ch:String ):Boolean {
461 return ( ch == ' ' || ch == '\t' );
465 * Determines if a character is a digit [0-9].
467 * @return True if the character passed in is a digit
469 private function isDigit( ch:String ):Boolean {
470 return ( ch >= '0' && ch <= '9' );
474 * Determines if a character is a digit [0-9].
476 * @return True if the character passed in is a digit
478 private function isHexDigit( ch:String ):Boolean {
479 // get the uppercase value of ch so we only have
480 // to compare the value between 'A' and 'F'
481 var uc:String = ch.toUpperCase();
483 // a hex digit is a digit of A-F, inclusive ( using
484 // our uppercase constraint )
485 return ( isDigit( ch ) || ( uc >= 'A' && uc <= 'F' ) );
489 * Raises a parsing error with a specified message, tacking
490 * on the error location and the original string.
492 * @param message The message indicating why the error occurred
494 public function parseError( message:String ):void {
495 throw new JSONParseError( message, loc, jsonString );