JavaScriptSerializer.as 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. JavaScriptSerializer.as
  3. Part of the Flash / JavaScript Integration Kit
  4. http://www.macromedia.com/go/flashjavascript
  5. Created by:
  6. Mike Chambers
  7. http://weblogs.macromedia.com/mesh/
  8. mesh@macromedia.com
  9. Christian Cantrell
  10. http://weblogs.macromedia.com/cantrell/
  11. cantrell@macromedia.com
  12. ----
  13. Macromedia(r) Flash(r)./ JavaScript Integration Kit License
  14. Copyright (c) 2005 Macromedia, inc. All rights reserved.
  15. Redistribution and use in source and binary forms, with or without modification,
  16. are permitted provided that the following conditions are met:
  17. 1. Redistributions of source code must retain the above copyright notice, this
  18. list of conditions and the following disclaimer.
  19. 2. Redistributions in binary form must reproduce the above copyright notice,
  20. this list of conditions and the following disclaimer in the documentation and/or
  21. other materials provided with the distribution.
  22. 3. The end-user documentation included with the redistribution, if any, must
  23. include the following acknowledgment:
  24. "This product includes software developed by Macromedia, Inc.
  25. (http://www.macromedia.com)."
  26. Alternately, this acknowledgment may appear in the software itself, if and
  27. wherever such third-party acknowledgments normally appear.
  28. 4. The name Macromedia must not be used to endorse or promote products derived
  29. from this software without prior written permission. For written permission,
  30. please contact devrelations@macromedia.com.
  31. 5. Products derived from this software may not be called "Macromedia" or
  32. “Macromedia Flash”, nor may "Macromedia" or “Macromedia Flash” appear in their
  33. name.
  34. THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MACROMEDIA OR
  37. ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  38. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  39. OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  40. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  42. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  43. DAMAGE.
  44. */
  45. class com.macromedia.javascript.JavaScriptSerializer
  46. {
  47. /**************** Serialization Methods ****************/
  48. /*
  49. Static function that serializes any supported data types.
  50. Returns a String
  51. */
  52. public static function serializeItem(item:Object):String
  53. {
  54. var type:String = typeof(item);
  55. var out:String;
  56. switch (type)
  57. {
  58. case "string":
  59. {
  60. out = JavaScriptSerializer.serializeString(String(item));
  61. break;
  62. }
  63. case "number":
  64. {
  65. out = JavaScriptSerializer.serializeNumber(Number(item));
  66. break;
  67. }
  68. case "boolean":
  69. {
  70. out = JavaScriptSerializer.serializeBoolean(Boolean(item));
  71. break;
  72. }
  73. case "null":
  74. {
  75. out = JavaScriptSerializer.serializeNull();
  76. break;
  77. }
  78. case "undefined":
  79. {
  80. out = JavaScriptSerializer.serializeUndefined();
  81. break;
  82. }
  83. case "object":
  84. {
  85. if(item instanceof Date)
  86. {
  87. out = JavaScriptSerializer.serializeDate(new Date(item.getTime()));
  88. }
  89. else if(item instanceof Array)
  90. {
  91. out = JavaScriptSerializer.serializeArray(item);
  92. }
  93. else
  94. {
  95. //treat it as regular Object
  96. out = JavaScriptSerializer.serializeObject(item);
  97. }
  98. break;
  99. }
  100. }
  101. return out;
  102. }
  103. /* Serializes an Object */
  104. public static function serializeObject(o:Object):String
  105. {
  106. var sb:String = "{";
  107. for(var x:String in o)
  108. {
  109. //dont include functions
  110. if(typeof(x[o]) == "function")
  111. {
  112. continue;
  113. }
  114. sb += x + ":" + serializeItem(o[x]) + ",";
  115. }
  116. //remove the trailing ","
  117. if(sb.substring(sb.length - 1) == ",")
  118. {
  119. sb = sb.substring(0, sb.length - 1);
  120. }
  121. sb += "}";
  122. return sb;
  123. }
  124. /* Serializes an Array */
  125. //not typed since I can't cast an object to Array
  126. public static function serializeArray(o):String
  127. {
  128. var len:Number = o.length;
  129. var sb:String = "[";
  130. for(var i:Number = 0; i < len; i++)
  131. {
  132. sb += serializeItem(o[i]);
  133. if(i != len - 1)
  134. {
  135. sb += ",";
  136. }
  137. }
  138. sb += "]";
  139. return sb;
  140. }
  141. /* Serializes a String */
  142. public static function serializeString(s:String):String
  143. {
  144. return "'" + s + "'";
  145. }
  146. /* Serializes a Number */
  147. public static function serializeNumber(n:Number):String
  148. {
  149. return String(n);
  150. }
  151. /* Serializes a Boolean value */
  152. public static function serializeBoolean(b:Boolean):String
  153. {
  154. return String(b);
  155. }
  156. /* Serializes undefined */
  157. public static function serializeUndefined(Void):String
  158. {
  159. return "undefined";
  160. }
  161. /* Serializes null */
  162. public static function serializeNull(Void):String
  163. {
  164. return "null";
  165. }
  166. /* Serializes a Date */
  167. public static function serializeDate(d:Date):String
  168. {
  169. return "new Date(" + d.getTime() + ")";
  170. }
  171. /**************** De-Serialization Methods ****************/
  172. /*
  173. Static function that de-serializes any supported data types.
  174. Returns a String
  175. */
  176. public static function deserializeItem(type:String, data:String):Object
  177. {
  178. var out:Object;
  179. switch (type)
  180. {
  181. case "str":
  182. {
  183. out = JavaScriptSerializer.deserializeString(data);
  184. break;
  185. }
  186. case "num":
  187. {
  188. out = JavaScriptSerializer.deserializeNumber(data);
  189. break;
  190. }
  191. case "bool":
  192. {
  193. out = JavaScriptSerializer.deserializeBoolean(data);
  194. break;
  195. }
  196. case "null":
  197. {
  198. out = JavaScriptSerializer.deserializeNull();
  199. break;
  200. }
  201. case "undf":
  202. {
  203. out = JavaScriptSerializer.deserializeUndefined();
  204. break;
  205. }
  206. case "date":
  207. {
  208. out = JavaScriptSerializer.deserializeDate(data);
  209. break;
  210. }
  211. case "xser":
  212. {
  213. out = JavaScriptSerializer.deserializeXMLSerializedItem(data);
  214. trace(data);
  215. break;
  216. }
  217. }
  218. return out;
  219. }
  220. /* Deserializes a String */
  221. public static function deserializeString(s:String):String
  222. {
  223. return s;
  224. }
  225. /* Deserializes a Number */
  226. public static function deserializeNumber(s:String):Number
  227. {
  228. return Number(s);
  229. }
  230. /* Deserializes a Boolean Value */
  231. public static function deserializeBoolean(s:String):String
  232. {
  233. return Boolean(s);
  234. }
  235. /* Deserializes undefined */
  236. //returns undefined
  237. public static function deserializeUndefined(s:String)
  238. {
  239. return undefined;
  240. }
  241. /* Deserializes null */
  242. //returns null
  243. public static function deserializeNull(s:String)
  244. {
  245. return null;
  246. }
  247. /* Deserializes a Date */
  248. public static function deserializeDate(s:String):Date
  249. {
  250. return new Date(Number(s));
  251. }
  252. /**************** De-Serialization XML Methods ****************/
  253. /*
  254. The methods below are for deserializing data serialized in XML format.
  255. This is used for serializing Objects and Arrays
  256. */
  257. /*
  258. Static function that de-serializes any supported XML serialized data types.
  259. Returns a String
  260. */
  261. public static function deserializeXMLSerializedItem(data:String):Object
  262. {
  263. var x:XML = new XML();
  264. x.ignoreWhite = true;
  265. x.parseXML(data);
  266. var out:Object = parseNode(x.firstChild.firstChild, new Object);
  267. return out;
  268. }
  269. /* recursive function that parses the xml tree */
  270. public static function parseNode(x:XMLNode, o:Object):Object
  271. {
  272. var nodeName:String = x.nodeName;
  273. var nodeValue:String = x.firstChild.nodeValue;
  274. var varName:String = x.attributes["name"];
  275. var children:Array = x.childNodes;
  276. var len:Number = children.length;
  277. switch(nodeName)
  278. {
  279. case "obj":
  280. {
  281. if(varName == null)
  282. {
  283. o = new Object();
  284. }
  285. else
  286. {
  287. o[varName] = new Object();
  288. }
  289. break;
  290. }
  291. case "str":
  292. {
  293. if(varName == undefined)
  294. {
  295. o = String(nodeValue);
  296. }
  297. else
  298. {
  299. o[varName] = nodeValue;
  300. }
  301. break;
  302. }
  303. case "num":
  304. {
  305. if(varName == null)
  306. {
  307. o = Number(nodeValue);
  308. }
  309. else
  310. {
  311. o[varName] = Number(nodeValue);
  312. }
  313. break;
  314. }
  315. case "bool":
  316. {
  317. if(varName == null)
  318. {
  319. o = Boolean(nodeValue);
  320. }
  321. else
  322. {
  323. o[varName] = Boolean(nodeValue);
  324. }
  325. break;
  326. }
  327. case "null":
  328. {
  329. if(varName == null)
  330. {
  331. o = null;
  332. }
  333. else
  334. {
  335. o[varName] = null;
  336. }
  337. break;
  338. }
  339. case "undf":
  340. {
  341. if(varName == null)
  342. {
  343. o = undefined;
  344. }
  345. else
  346. {
  347. o[varName] = undefined;
  348. }
  349. break;
  350. }
  351. case "date":
  352. {
  353. if(varName == null)
  354. {
  355. o = new Date(Number(nodeValue));
  356. }
  357. else
  358. {
  359. o[varName] = new Date(Number(nodeValue));
  360. }
  361. break;
  362. }
  363. case "array":
  364. {
  365. //this is not typed because the compiler gets confused about
  366. //the explicit type change for o below.
  367. var arr;
  368. if(varName == null)
  369. {
  370. o = new Array();
  371. arr = o;
  372. }
  373. else
  374. {
  375. o[varName] = new Array();
  376. arr = o[varName];
  377. }
  378. for(var x:Number = 0; x < len; x++)
  379. {
  380. arr.push(parseNode(children[x], o));
  381. }
  382. return arr;
  383. }
  384. }
  385. for(var i:Number = 0; i < len; i++)
  386. {
  387. parseNode(children[i], o);
  388. }
  389. return o;
  390. }
  391. }