|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | +package { |
| 5 | +import flash.display.MovieClip; |
| 6 | +public class Test extends MovieClip {} |
| 7 | +} |
| 8 | + |
| 9 | + |
| 10 | + import avmplus.*; |
| 11 | + import flash.utils.*; |
| 12 | +import com.adobe.test.Assert; |
| 13 | + |
| 14 | +// var SECTION = "15.2"; |
| 15 | +// var VERSION = "ECMA_5"; |
| 16 | +// var TITLE = "JSON AS3 specific types"; |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +function removeExceptionDetail(s:String) { |
| 21 | + var fnd=s.indexOf(" "); |
| 22 | + if (fnd>-1) { |
| 23 | + if (s.indexOf(':',fnd)>-1) { |
| 24 | + s=s.substring(0,s.indexOf(':',fnd)); |
| 25 | + } |
| 26 | + } |
| 27 | + return s; |
| 28 | +} |
| 29 | + |
| 30 | +function sortObject(o:Object) { |
| 31 | + var keys=[]; |
| 32 | + var key; |
| 33 | + for ( key in o ) { |
| 34 | + if (o[key]===undefined) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + keys[keys.length]=key; |
| 38 | + } |
| 39 | + keys.sort(); |
| 40 | + var ret="{"; |
| 41 | + var value; |
| 42 | + for (var i in keys) { |
| 43 | + key=keys[i]; |
| 44 | + value=o[key]; |
| 45 | + if (value is String) { |
| 46 | + value='"'+value+'"'; |
| 47 | + } else if (value is Array) { |
| 48 | + value='['+value+']'; |
| 49 | + } else if (value is Object) { |
| 50 | + } |
| 51 | + ret += '"'+key+'":'+value+","; |
| 52 | + } |
| 53 | + ret=ret.substring(0,ret.length-1); |
| 54 | + ret+="}"; |
| 55 | + return ret; |
| 56 | +} |
| 57 | + |
| 58 | + // Vector tests |
| 59 | + |
| 60 | + Assert.expectEq("Vectors are stringified to JSON Array syntax",'[1,"2",true,4,"a"]',JSON.stringify(Vector.<*>([1,"2",true,4.0,"a"]))); |
| 61 | + Assert.expectEq("Vectors of int are stringified to JSON Array syntax",'[1,-2,3,-4]',JSON.stringify(Vector.<int>([1,-2,3,-4]))); |
| 62 | + Assert.expectEq("Vectors of uint are stringified to JSON Array syntax",'[1,2,3,4]',JSON.stringify(Vector.<uint>([1,2,3,4]))); |
| 63 | + Assert.expectEq("Vectors of Number are stringified to JSON Array syntax",'[1,2.2,3.33,4.444]',JSON.stringify(Vector.<Number>([1,2.2,3.33,4.444]))); |
| 64 | + Assert.expectEq("Vectors of Boolean are stringified to JSON Array syntax",'[true,false,true,false]',JSON.stringify(Vector.<Boolean>([true,false,true,false]))); |
| 65 | + Assert.expectEq("uninitialized Vector is stringified to []","[]",JSON.stringify(new Vector.<*>())); |
| 66 | + Assert.expectEq("Vector of edge case values",'[null,null,null,null,""]',JSON.stringify(Vector.<*>([null,undefined,-Infinity,NaN,""]))); |
| 67 | + |
| 68 | + // Array tests |
| 69 | + Assert.expectEq("Arrays are parsed correctly, empty []","",JSON.parse("[]").toString()); |
| 70 | + Assert.expectEq("Arrays are parsed correctly, [1]","1",JSON.parse("[1]").toString()); |
| 71 | + Assert.expectEq("Arrays are parsed correctly, [1,2,3]","1,2,3",JSON.parse("[1,2,3]").toString()); |
| 72 | + |
| 73 | + JSONSyntaxError="SyntaxError: Error #1132"; |
| 74 | + |
| 75 | +// crashes |
| 76 | + exception1="no exception"; |
| 77 | + try { |
| 78 | + JSON.parse("["); |
| 79 | + } catch(e) { |
| 80 | + exception1=removeExceptionDetail(e.toString()); |
| 81 | + } |
| 82 | + Assert.expectEq("Arrays errors are detected, [",JSONSyntaxError,exception1); |
| 83 | + |
| 84 | + exception2="no exception"; |
| 85 | + try { |
| 86 | + JSON.parse("[1,2,3,fals]"); |
| 87 | + } catch(e) { |
| 88 | + exception2=removeExceptionDetail(e.toString()); |
| 89 | + } |
| 90 | + Assert.expectEq("Arrays errors are detected, [1,2,3,fals]",JSONSyntaxError,exception2); |
| 91 | + |
| 92 | + exception3="no exception"; |
| 93 | + try { |
| 94 | + JSON.parse("[1,2,]"); |
| 95 | + } catch(e) { |
| 96 | + exception3=removeExceptionDetail(e.toString()); |
| 97 | + } |
| 98 | + Assert.expectEq("Arrays errors are detected, [1,2,]",JSONSyntaxError,exception3); |
| 99 | + |
| 100 | + exception4="no exception"; |
| 101 | + try { |
| 102 | + JSON.parse("[1,2,3"); |
| 103 | + } catch(e) { |
| 104 | + exception4=removeExceptionDetail(e.toString()); |
| 105 | + } |
| 106 | + Assert.expectEq("Arrays errors are detected, [1,2,3",JSONSyntaxError,exception4); |
| 107 | + |
| 108 | + var star:*="anytype"; |
| 109 | + Assert.expectEq("Type * is like an untyped variable",'"anytype"',JSON.stringify(star)); |
| 110 | + |
| 111 | + |
| 112 | + // Dictionary tests |
| 113 | + var d:Dictionary=new Dictionary(); |
| 114 | + |
| 115 | + class Foo extends Object { |
| 116 | + static var timecounter = 1; |
| 117 | + public var timestamp; |
| 118 | + var name; |
| 119 | + function Foo(name) { this.name = name; this.timestamp = timecounter++; } |
| 120 | + public function toString():String { return this.name; } |
| 121 | + } |
| 122 | + var o1 = new Foo("o"); |
| 123 | + var p2 = new Foo("p"); |
| 124 | + var o3 = new Foo("o"); |
| 125 | + d[o3]="value"; |
| 126 | + Assert.expectEq("stringify a Dictionary object",'"Dictionary"',JSON.stringify(d)); |
| 127 | + |
| 128 | + // Change toJSON to show object values, where distinct objects |
| 129 | + // with identical toString output are distinguished (if possible) |
| 130 | + // by the timestamps they were constructed with. |
| 131 | + var origDictionarytoJSON=Dictionary.prototype.toJSON; |
| 132 | + Dictionary.prototype.toJSON=function():* { |
| 133 | + var x = {}; |
| 134 | + for (var i in this) { |
| 135 | + if ("timestamp" in i) { |
| 136 | + x[String(i)+"_"+i.timestamp] = this[i]; |
| 137 | + } else { |
| 138 | + x[String(i)] = this[i]; |
| 139 | + } |
| 140 | + } |
| 141 | + return x; // x is not a String! |
| 142 | + }; |
| 143 | + |
| 144 | + // The simple case: a single element Dictionary won't run into |
| 145 | + // issues of unspecified order of rendered key/value entries. |
| 146 | + Assert.expectEq("stringify a 1-elem Dictionary with customized toJSON", |
| 147 | + '{"o_3":"value"}', |
| 148 | + JSON.stringify(d)); |
| 149 | + |
| 150 | + d[o1]="o1-value"; |
| 151 | + d[p2]="p2-value"; |
| 152 | + d[o3]="o3-value"; |
| 153 | + |
| 154 | + // Trickier case: multi-element Dictionary may present entries in |
| 155 | + // arbitrary order; normalize it by reparsing and sorting entries. |
| 156 | + Assert.expectEq("stringify a 3-elem Dictionary with customized toJSON", |
| 157 | + '{"o_1":"o1-value","o_3":"o3-value","p_2":"p2-value"}', |
| 158 | + sortObject(JSON.parse(JSON.stringify(d)))); |
| 159 | + |
| 160 | + // Restore original toJSON and its trivial behavior. |
| 161 | + Dictionary.prototype.toJSON=origDictionarytoJSON; |
| 162 | + Assert.expectEq("stringify a Dictionary restored original toJSON",'"Dictionary"',JSON.stringify(d)); |
| 163 | + |
| 164 | + // ByteArray tests |
| 165 | + var b:ByteArray=new ByteArray(); |
| 166 | + b.writeUTF("byte array string"); |
| 167 | + Assert.expectEq("stringify a ByteArray object",'"ByteArray"',JSON.stringify(b)); |
| 168 | + |
| 169 | + var origByteArraytoJSON=ByteArray.prototype.toJSON; |
| 170 | + ByteArray.prototype.toJSON=function() { |
| 171 | + return this.toString().substring(2); |
| 172 | + } |
| 173 | + Assert.expectEq("stringify a ByteArray object with custom toJSON",'"byte array string"',JSON.stringify(b)); |
| 174 | + ByteArray.prototype.toJSON=origByteArraytoJSON; |
| 175 | + Assert.expectEq("stringify a ByteArray object with restored toJSON",'"ByteArray"',JSON.stringify(b)); |
| 176 | + |
| 177 | + // XML |
| 178 | + |
| 179 | + var x:XML=<root><element1 prop1="one"/></root>; |
| 180 | + Assert.expectEq("stringify XML object",'"XML"',JSON.stringify(x)); |
| 181 | + |
| 182 | + // Date |
| 183 | + var dt:Date=new Date(2011,3,26,10,33,0,111); |
| 184 | + Assert.expectEq("stringify a Date object",true,JSON.stringify(dt).indexOf('"Tue Apr 26 10:33:00')>-1); |
| 185 | + var origDatetoJSON=Date.prototype.toJSON; |
| 186 | + Date.prototype.toJSON=function() { |
| 187 | + return ""+this.getFullYear()+"-"+(this.getMonth()+1)+"-"+this.getDate()+"T"+this.getHours()+":"+this.getMinutes()+":"+this.getSeconds()+"."+this.getMilliseconds()+"Z"; |
| 188 | + } |
| 189 | + Assert.expectEq("stringify a Date object with custom toJSON",'"2011-4-26T10:33:0.111Z"',JSON.stringify(dt)); |
| 190 | + Date.prototype.toJSON=origDatetoJSON; |
| 191 | + Assert.expectEq("stringify a Date object with restored toJSON",true,JSON.stringify(dt).indexOf('"Tue Apr 26 10:33:00')>-1); |
| 192 | + |
| 193 | + |
0 commit comments