82ade196af43a8d69e6da8a7221006d6655dfbe2
[redakcja.git] / apps / filebrowser / media / filebrowser / uploadify / com / adobe / serialization / json / JSONDecoder.as
1 /*\r
2   Copyright (c) 2008, Adobe Systems Incorporated\r
3   All rights reserved.\r
4 \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
7   met:\r
8 \r
9   * Redistributions of source code must retain the above copyright notice, \r
10     this list of conditions and the following disclaimer.\r
11   \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
15   \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
19 \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
31 */\r
32 \r
33 package com.adobe.serialization.json {\r
34 \r
35         public class JSONDecoder {\r
36                 \r
37                 /** The value that will get parsed from the JSON string */\r
38                 private var value:*;\r
39                 \r
40                 /** The tokenizer designated to read the JSON string */\r
41                 private var tokenizer:JSONTokenizer;\r
42                 \r
43                 /** The current token from the tokenizer */\r
44                 private var token:JSONToken;\r
45                 \r
46                 /**\r
47                  * Constructs a new JSONDecoder to parse a JSON string \r
48                  * into a native object.\r
49                  *\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
54                  * @tiptext\r
55                  */\r
56                 public function JSONDecoder( s:String ) {\r
57                         \r
58                         tokenizer = new JSONTokenizer( s );\r
59                         \r
60                         nextToken();\r
61                         value = parseValue();\r
62                 }\r
63                 \r
64                 /**\r
65                  * Gets the internal object that was created by parsing\r
66                  * the JSON string passed to the constructor.\r
67                  *\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
72                  * @tiptext\r
73                  */\r
74                 public function getValue():* {\r
75                         return value;\r
76                 }\r
77                 \r
78                 /**\r
79                  * Returns the next token from the tokenzier reading\r
80                  * the JSON string\r
81                  */\r
82                 private function nextToken():JSONToken {\r
83                         return token = tokenizer.getNextToken();\r
84                 }\r
85                 \r
86                 /**\r
87                  * Attempt to parse an array\r
88                  */\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
93                         \r
94                         // grab the next token from the tokenizer to move\r
95                         // past the opening [\r
96                         nextToken();\r
97                         \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
101                                 return a;\r
102                         }\r
103                         \r
104                         // deal with elements of the array, and use an "infinite"\r
105                         // loop because we could have any amount of elements\r
106                         while ( true ) {\r
107                                 // read in the value and add it to the array\r
108                                 a.push ( parseValue() );\r
109                         \r
110                                 // after the value there should be a ] or a ,\r
111                                 nextToken();\r
112                                 \r
113                                 if ( token.type == JSONTokenType.RIGHT_BRACKET ) {\r
114                                         // we're done reading the array, so return it\r
115                                         return a;\r
116                                 } else if ( token.type == JSONTokenType.COMMA ) {\r
117                                         // move past the comma and read another value\r
118                                         nextToken();\r
119                                 } else {\r
120                                         tokenizer.parseError( "Expecting ] or , but found " + token.value );\r
121                                 }\r
122                         }\r
123             return null;\r
124                 }\r
125                 \r
126                 /**\r
127                  * Attempt to parse an object\r
128                  */\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
133                                                 \r
134                         // store the string part of an object member so\r
135                         // that we can assign it a value in the object\r
136                         var key:String\r
137                         \r
138                         // grab the next token from the tokenizer\r
139                         nextToken();\r
140                         \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
144                                 return o;\r
145                         }\r
146                         \r
147                         // deal with members of the object, and use an "infinite"\r
148                         // loop because we could have any amount of members\r
149                         while ( true ) {\r
150                         \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
154                                         \r
155                                         // move past the string to see what's next\r
156                                         nextToken();\r
157                                         \r
158                                         // after the string there should be a :\r
159                                         if ( token.type == JSONTokenType.COLON ) {\r
160                                                 \r
161                                                 // move past the : and read/assign a value for the key\r
162                                                 nextToken();\r
163                                                 o[key] = parseValue();  \r
164                                                 \r
165                                                 // move past the value to see what's next\r
166                                                 nextToken();\r
167                                                 \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
171                                                         return o;\r
172                                                         \r
173                                                 } else if ( token.type == JSONTokenType.COMMA ) {\r
174                                                         // skip past the comma and read another member\r
175                                                         nextToken();\r
176                                                 } else {\r
177                                                         tokenizer.parseError( "Expecting } or , but found " + token.value );\r
178                                                 }\r
179                                         } else {\r
180                                                 tokenizer.parseError( "Expecting : but found " + token.value );\r
181                                         }\r
182                                 } else {\r
183                                         tokenizer.parseError( "Expecting string but found " + token.value );\r
184                                 }\r
185                         }\r
186             return null;\r
187                 }\r
188                 \r
189                 /**\r
190                  * Attempt to parse a value\r
191                  */\r
192                 private function parseValue():Object\r
193                 {\r
194                         // Catch errors when the input stream ends abruptly\r
195                         if ( token == null )\r
196                         {\r
197                                 tokenizer.parseError( "Unexpected end of input" );\r
198                         }\r
199                                         \r
200                         switch ( token.type ) {\r
201                                 case JSONTokenType.LEFT_BRACE:\r
202                                         return parseObject();\r
203                                         \r
204                                 case JSONTokenType.LEFT_BRACKET:\r
205                                         return parseArray();\r
206                                         \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
213 \r
214                                 default:\r
215                                         tokenizer.parseError( "Unexpected " + token.value );\r
216                                         \r
217                         }\r
218             return null;\r
219                 }\r
220         }\r
221 }\r