2 Copyright (c) 2008, Adobe Systems Incorporated
\r
5 Redistribution and use in source and binary forms, with or without
\r
6 modification, are permitted provided that the following conditions are
\r
9 * Redistributions of source code must retain the above copyright notice,
\r
10 this list of conditions and the following disclaimer.
\r
12 * Redistributions in binary form must reproduce the above copyright
\r
13 notice, this list of conditions and the following disclaimer in the
\r
14 documentation and/or other materials provided with the distribution.
\r
16 * Neither the name of Adobe Systems Incorporated nor the names of its
\r
17 contributors may be used to endorse or promote products derived from
\r
18 this software without specific prior written permission.
\r
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
\r
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
\r
22 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
\r
23 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
\r
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
\r
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
\r
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
\r
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
\r
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
\r
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
33 package com.adobe.serialization.json {
\r
35 public class JSONDecoder {
\r
37 /** The value that will get parsed from the JSON string */
\r
38 private var value:*;
\r
40 /** The tokenizer designated to read the JSON string */
\r
41 private var tokenizer:JSONTokenizer;
\r
43 /** The current token from the tokenizer */
\r
44 private var token:JSONToken;
\r
47 * Constructs a new JSONDecoder to parse a JSON string
\r
48 * into a native object.
\r
50 * @param s The JSON string to be converted
\r
51 * into a native object
\r
52 * @langversion ActionScript 3.0
\r
53 * @playerversion Flash 9.0
\r
56 public function JSONDecoder( s:String ) {
\r
58 tokenizer = new JSONTokenizer( s );
\r
61 value = parseValue();
\r
65 * Gets the internal object that was created by parsing
\r
66 * the JSON string passed to the constructor.
\r
68 * @return The internal object representation of the JSON
\r
69 * string that was passed to the constructor
\r
70 * @langversion ActionScript 3.0
\r
71 * @playerversion Flash 9.0
\r
74 public function getValue():* {
\r
79 * Returns the next token from the tokenzier reading
\r
82 private function nextToken():JSONToken {
\r
83 return token = tokenizer.getNextToken();
\r
87 * Attempt to parse an array
\r
89 private function parseArray():Array {
\r
90 // create an array internally that we're going to attempt
\r
91 // to parse from the tokenizer
\r
92 var a:Array = new Array();
\r
94 // grab the next token from the tokenizer to move
\r
95 // past the opening [
\r
98 // check to see if we have an empty array
\r
99 if ( token.type == JSONTokenType.RIGHT_BRACKET ) {
\r
100 // we're done reading the array, so return it
\r
104 // deal with elements of the array, and use an "infinite"
\r
105 // loop because we could have any amount of elements
\r
107 // read in the value and add it to the array
\r
108 a.push ( parseValue() );
\r
110 // after the value there should be a ] or a ,
\r
113 if ( token.type == JSONTokenType.RIGHT_BRACKET ) {
\r
114 // we're done reading the array, so return it
\r
116 } else if ( token.type == JSONTokenType.COMMA ) {
\r
117 // move past the comma and read another value
\r
120 tokenizer.parseError( "Expecting ] or , but found " + token.value );
\r
127 * Attempt to parse an object
\r
129 private function parseObject():Object {
\r
130 // create the object internally that we're going to
\r
131 // attempt to parse from the tokenizer
\r
132 var o:Object = new Object();
\r
134 // store the string part of an object member so
\r
135 // that we can assign it a value in the object
\r
138 // grab the next token from the tokenizer
\r
141 // check to see if we have an empty object
\r
142 if ( token.type == JSONTokenType.RIGHT_BRACE ) {
\r
143 // we're done reading the object, so return it
\r
147 // deal with members of the object, and use an "infinite"
\r
148 // loop because we could have any amount of members
\r
151 if ( token.type == JSONTokenType.STRING ) {
\r
152 // the string value we read is the key for the object
\r
153 key = String( token.value );
\r
155 // move past the string to see what's next
\r
158 // after the string there should be a :
\r
159 if ( token.type == JSONTokenType.COLON ) {
\r
161 // move past the : and read/assign a value for the key
\r
163 o[key] = parseValue();
\r
165 // move past the value to see what's next
\r
168 // after the value there's either a } or a ,
\r
169 if ( token.type == JSONTokenType.RIGHT_BRACE ) {
\r
170 // // we're done reading the object, so return it
\r
173 } else if ( token.type == JSONTokenType.COMMA ) {
\r
174 // skip past the comma and read another member
\r
177 tokenizer.parseError( "Expecting } or , but found " + token.value );
\r
180 tokenizer.parseError( "Expecting : but found " + token.value );
\r
183 tokenizer.parseError( "Expecting string but found " + token.value );
\r
190 * Attempt to parse a value
\r
192 private function parseValue():Object
\r
194 // Catch errors when the input stream ends abruptly
\r
195 if ( token == null )
\r
197 tokenizer.parseError( "Unexpected end of input" );
\r
200 switch ( token.type ) {
\r
201 case JSONTokenType.LEFT_BRACE:
\r
202 return parseObject();
\r
204 case JSONTokenType.LEFT_BRACKET:
\r
205 return parseArray();
\r
207 case JSONTokenType.STRING:
\r
208 case JSONTokenType.NUMBER:
\r
209 case JSONTokenType.TRUE:
\r
210 case JSONTokenType.FALSE:
\r
211 case JSONTokenType.NULL:
\r
212 return token.value;
\r
215 tokenizer.parseError( "Unexpected " + token.value );
\r