--- /dev/null
+/*\r
+ Copyright (c) 2008, Adobe Systems Incorporated\r
+ All rights reserved.\r
+\r
+ Redistribution and use in source and binary forms, with or without \r
+ modification, are permitted provided that the following conditions are\r
+ met:\r
+\r
+ * Redistributions of source code must retain the above copyright notice, \r
+ this list of conditions and the following disclaimer.\r
+ \r
+ * Redistributions in binary form must reproduce the above copyright\r
+ notice, this list of conditions and the following disclaimer in the \r
+ documentation and/or other materials provided with the distribution.\r
+ \r
+ * Neither the name of Adobe Systems Incorporated nor the names of its \r
+ contributors may be used to endorse or promote products derived from \r
+ this software without specific prior written permission.\r
+\r
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS\r
+ IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR \r
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+*/\r
+\r
+package com.adobe.serialization.json {\r
+\r
+ public class JSONDecoder {\r
+ \r
+ /** The value that will get parsed from the JSON string */\r
+ private var value:*;\r
+ \r
+ /** The tokenizer designated to read the JSON string */\r
+ private var tokenizer:JSONTokenizer;\r
+ \r
+ /** The current token from the tokenizer */\r
+ private var token:JSONToken;\r
+ \r
+ /**\r
+ * Constructs a new JSONDecoder to parse a JSON string \r
+ * into a native object.\r
+ *\r
+ * @param s The JSON string to be converted\r
+ * into a native object\r
+ * @langversion ActionScript 3.0\r
+ * @playerversion Flash 9.0\r
+ * @tiptext\r
+ */\r
+ public function JSONDecoder( s:String ) {\r
+ \r
+ tokenizer = new JSONTokenizer( s );\r
+ \r
+ nextToken();\r
+ value = parseValue();\r
+ }\r
+ \r
+ /**\r
+ * Gets the internal object that was created by parsing\r
+ * the JSON string passed to the constructor.\r
+ *\r
+ * @return The internal object representation of the JSON\r
+ * string that was passed to the constructor\r
+ * @langversion ActionScript 3.0\r
+ * @playerversion Flash 9.0\r
+ * @tiptext\r
+ */\r
+ public function getValue():* {\r
+ return value;\r
+ }\r
+ \r
+ /**\r
+ * Returns the next token from the tokenzier reading\r
+ * the JSON string\r
+ */\r
+ private function nextToken():JSONToken {\r
+ return token = tokenizer.getNextToken();\r
+ }\r
+ \r
+ /**\r
+ * Attempt to parse an array\r
+ */\r
+ private function parseArray():Array {\r
+ // create an array internally that we're going to attempt\r
+ // to parse from the tokenizer\r
+ var a:Array = new Array();\r
+ \r
+ // grab the next token from the tokenizer to move\r
+ // past the opening [\r
+ nextToken();\r
+ \r
+ // check to see if we have an empty array\r
+ if ( token.type == JSONTokenType.RIGHT_BRACKET ) {\r
+ // we're done reading the array, so return it\r
+ return a;\r
+ }\r
+ \r
+ // deal with elements of the array, and use an "infinite"\r
+ // loop because we could have any amount of elements\r
+ while ( true ) {\r
+ // read in the value and add it to the array\r
+ a.push ( parseValue() );\r
+ \r
+ // after the value there should be a ] or a ,\r
+ nextToken();\r
+ \r
+ if ( token.type == JSONTokenType.RIGHT_BRACKET ) {\r
+ // we're done reading the array, so return it\r
+ return a;\r
+ } else if ( token.type == JSONTokenType.COMMA ) {\r
+ // move past the comma and read another value\r
+ nextToken();\r
+ } else {\r
+ tokenizer.parseError( "Expecting ] or , but found " + token.value );\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+ \r
+ /**\r
+ * Attempt to parse an object\r
+ */\r
+ private function parseObject():Object {\r
+ // create the object internally that we're going to\r
+ // attempt to parse from the tokenizer\r
+ var o:Object = new Object();\r
+ \r
+ // store the string part of an object member so\r
+ // that we can assign it a value in the object\r
+ var key:String\r
+ \r
+ // grab the next token from the tokenizer\r
+ nextToken();\r
+ \r
+ // check to see if we have an empty object\r
+ if ( token.type == JSONTokenType.RIGHT_BRACE ) {\r
+ // we're done reading the object, so return it\r
+ return o;\r
+ }\r
+ \r
+ // deal with members of the object, and use an "infinite"\r
+ // loop because we could have any amount of members\r
+ while ( true ) {\r
+ \r
+ if ( token.type == JSONTokenType.STRING ) {\r
+ // the string value we read is the key for the object\r
+ key = String( token.value );\r
+ \r
+ // move past the string to see what's next\r
+ nextToken();\r
+ \r
+ // after the string there should be a :\r
+ if ( token.type == JSONTokenType.COLON ) {\r
+ \r
+ // move past the : and read/assign a value for the key\r
+ nextToken();\r
+ o[key] = parseValue(); \r
+ \r
+ // move past the value to see what's next\r
+ nextToken();\r
+ \r
+ // after the value there's either a } or a ,\r
+ if ( token.type == JSONTokenType.RIGHT_BRACE ) {\r
+ // // we're done reading the object, so return it\r
+ return o;\r
+ \r
+ } else if ( token.type == JSONTokenType.COMMA ) {\r
+ // skip past the comma and read another member\r
+ nextToken();\r
+ } else {\r
+ tokenizer.parseError( "Expecting } or , but found " + token.value );\r
+ }\r
+ } else {\r
+ tokenizer.parseError( "Expecting : but found " + token.value );\r
+ }\r
+ } else {\r
+ tokenizer.parseError( "Expecting string but found " + token.value );\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+ \r
+ /**\r
+ * Attempt to parse a value\r
+ */\r
+ private function parseValue():Object\r
+ {\r
+ // Catch errors when the input stream ends abruptly\r
+ if ( token == null )\r
+ {\r
+ tokenizer.parseError( "Unexpected end of input" );\r
+ }\r
+ \r
+ switch ( token.type ) {\r
+ case JSONTokenType.LEFT_BRACE:\r
+ return parseObject();\r
+ \r
+ case JSONTokenType.LEFT_BRACKET:\r
+ return parseArray();\r
+ \r
+ case JSONTokenType.STRING:\r
+ case JSONTokenType.NUMBER:\r
+ case JSONTokenType.TRUE:\r
+ case JSONTokenType.FALSE:\r
+ case JSONTokenType.NULL:\r
+ return token.value;\r
+\r
+ default:\r
+ tokenizer.parseError( "Unexpected " + token.value );\r
+ \r
+ }\r
+ return null;\r
+ }\r
+ }\r
+}\r