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 JSONDecoder {
43 /** The object that will get parsed from the JSON string */
44 private var obj:Object;
46 /** The tokenizer designated to read the JSON string */
47 private var tokenizer:JSONTokenizer;
49 /** The current token from the tokenizer */
50 private var token:JSONToken;
53 * Constructs a new JSONDecoder to parse a JSON string
54 * into a native object.
56 * @param s The JSON string to be converted
57 * into a native object
58 * @langversion ActionScript 3.0
59 * @playerversion Flash 8.5
62 public function JSONDecoder( s:String ) {
64 tokenizer = new JSONTokenizer( s );
72 * Gets the internal object that was created by parsing
73 * the JSON string passed to the constructor.
75 * @return The internal object representation of the JSON
76 * string that was passed to the constructor
77 * @langversion ActionScript 3.0
78 * @playerversion Flash 8.5
81 public function getObject():Object {
87 * Returns the next token from the tokenzier reading
90 private function nextToken():JSONToken {
91 return token = tokenizer.getNextToken();
95 * Attempt to parse an array
97 private function parseArray():Array {
98 // create an array internally that we're going to attempt
99 // to parse from the tokenizer
100 var a:Array = new Array();
102 // grab the next token from the tokenizer to move
103 // past the opening [
106 // check to see if we have an empty array
107 if ( token.type == JSONTokenType.RIGHT_BRACKET ) {
108 // we're done reading the array, so return it
112 // deal with elements of the array, and use an "infinite"
113 // loop because we could have any amount of elements
115 // read in the value and add it to the array
116 a.push ( parseValue() );
118 // after the value there should be a ] or a ,
121 if ( token.type == JSONTokenType.RIGHT_BRACKET ) {
122 // we're done reading the array, so return it
124 } else if ( token.type == JSONTokenType.COMMA ) {
125 // move past the comma and read another value
128 tokenizer.parseError( "Expecting ] or , but found " + token.value );
135 * Attempt to parse an object
137 private function parseObject():Object {
138 // create the object internally that we're going to
139 // attempt to parse from the tokenizer
140 var o:Object = new Object();
142 // store the string part of an object member so
143 // that we can assign it a value in the object
146 // grab the next token from the tokenizer
149 // check to see if we have an empty object
150 if ( token.type == JSONTokenType.RIGHT_BRACE ) {
151 // we're done reading the object, so return it
155 // deal with members of the object, and use an "infinite"
156 // loop because we could have any amount of members
159 if ( token.type == JSONTokenType.STRING ) {
160 // the string value we read is the key for the object
161 key = String( token.value );
163 // move past the string to see what's next
166 // after the string there should be a :
167 if ( token.type == JSONTokenType.COLON ) {
169 // move past the : and read/assign a value for the key
171 o[key] = parseValue();
173 // move past the value to see what's next
176 // after the value there's either a } or a ,
177 if ( token.type == JSONTokenType.RIGHT_BRACE ) {
178 // // we're done reading the object, so return it
181 } else if ( token.type == JSONTokenType.COMMA ) {
182 // skip past the comma and read another member
185 tokenizer.parseError( "Expecting } or , but found " + token.value );
188 tokenizer.parseError( "Expecting : but found " + token.value );
191 tokenizer.parseError( "Expecting string but found " + token.value );
198 * Attempt to parse a value
200 private function parseValue():Object {
202 switch ( token.type ) {
203 case JSONTokenType.LEFT_BRACE:
204 return parseObject();
206 case JSONTokenType.LEFT_BRACKET:
209 case JSONTokenType.STRING:
210 case JSONTokenType.NUMBER:
211 case JSONTokenType.TRUE:
212 case JSONTokenType.FALSE:
213 case JSONTokenType.NULL:
217 tokenizer.parseError( "Unexpected " + token.value );