as3corelib 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. data/Gemfile +5 -0
  2. data/Gemfile.lock +28 -0
  3. data/README.textile +79 -0
  4. data/Rakefile +3 -0
  5. data/as3corelib.gemspec +23 -0
  6. data/build/build.properties +48 -0
  7. data/build/build.xml +104 -0
  8. data/examples/JSONExample/JSONExample.mxml +73 -0
  9. data/lib/as3corelib.rb +15 -0
  10. data/src/com/adobe/air/crypto/EncryptionKeyGenerator.as +313 -0
  11. data/src/com/adobe/air/filesystem/FileMonitor.as +245 -0
  12. data/src/com/adobe/air/filesystem/FileUtil.as +63 -0
  13. data/src/com/adobe/air/filesystem/VolumeMonitor.as +184 -0
  14. data/src/com/adobe/air/filesystem/events/FileMonitorEvent.as +61 -0
  15. data/src/com/adobe/air/logging/FileTarget.as +95 -0
  16. data/src/com/adobe/air/net/ResourceCache.as +165 -0
  17. data/src/com/adobe/air/net/events/ResourceCacheEvent.as +70 -0
  18. data/src/com/adobe/crypto/HMAC.as +127 -0
  19. data/src/com/adobe/crypto/MD5.as +281 -0
  20. data/src/com/adobe/crypto/MD5Stream.as +402 -0
  21. data/src/com/adobe/crypto/SHA1.as +289 -0
  22. data/src/com/adobe/crypto/SHA224.as +257 -0
  23. data/src/com/adobe/crypto/SHA256.as +261 -0
  24. data/src/com/adobe/crypto/WSSEUsernameToken.as +114 -0
  25. data/src/com/adobe/errors/IllegalStateError.as +63 -0
  26. data/src/com/adobe/fileformats/vcard/Address.as +47 -0
  27. data/src/com/adobe/fileformats/vcard/Email.as +39 -0
  28. data/src/com/adobe/fileformats/vcard/Phone.as +39 -0
  29. data/src/com/adobe/fileformats/vcard/VCard.as +54 -0
  30. data/src/com/adobe/fileformats/vcard/VCardParser.as +246 -0
  31. data/src/com/adobe/images/BitString.as +39 -0
  32. data/src/com/adobe/images/JPGEncoder.as +648 -0
  33. data/src/com/adobe/images/PNGEncoder.as +141 -0
  34. data/src/com/adobe/net/DynamicURLLoader.as +55 -0
  35. data/src/com/adobe/net/IURIResolver.as +76 -0
  36. data/src/com/adobe/net/MimeTypeMap.as +200 -0
  37. data/src/com/adobe/net/URI.as +2466 -0
  38. data/src/com/adobe/net/URIEncodingBitmap.as +139 -0
  39. data/src/com/adobe/net/proxies/RFC2817Socket.as +198 -0
  40. data/src/com/adobe/protocols/dict/Database.as +66 -0
  41. data/src/com/adobe/protocols/dict/Definition.as +71 -0
  42. data/src/com/adobe/protocols/dict/Dict.as +360 -0
  43. data/src/com/adobe/protocols/dict/DictionaryServer.as +60 -0
  44. data/src/com/adobe/protocols/dict/MatchStrategy.as +66 -0
  45. data/src/com/adobe/protocols/dict/Response.as +71 -0
  46. data/src/com/adobe/protocols/dict/events/ConnectedEvent.as +53 -0
  47. data/src/com/adobe/protocols/dict/events/DatabaseEvent.as +67 -0
  48. data/src/com/adobe/protocols/dict/events/DefinitionEvent.as +70 -0
  49. data/src/com/adobe/protocols/dict/events/DefinitionHeaderEvent.as +69 -0
  50. data/src/com/adobe/protocols/dict/events/DictionaryServerEvent.as +69 -0
  51. data/src/com/adobe/protocols/dict/events/DisconnectedEvent.as +55 -0
  52. data/src/com/adobe/protocols/dict/events/ErrorEvent.as +80 -0
  53. data/src/com/adobe/protocols/dict/events/MatchEvent.as +67 -0
  54. data/src/com/adobe/protocols/dict/events/MatchStrategiesEvent.as +70 -0
  55. data/src/com/adobe/protocols/dict/events/NoMatchEvent.as +54 -0
  56. data/src/com/adobe/protocols/dict/util/CompleteResponseEvent.as +68 -0
  57. data/src/com/adobe/protocols/dict/util/SocketHelper.as +81 -0
  58. data/src/com/adobe/serialization/json/JSON.as +86 -0
  59. data/src/com/adobe/serialization/json/JSONDecoder.as +327 -0
  60. data/src/com/adobe/serialization/json/JSONEncoder.as +312 -0
  61. data/src/com/adobe/serialization/json/JSONParseError.as +87 -0
  62. data/src/com/adobe/serialization/json/JSONToken.as +104 -0
  63. data/src/com/adobe/serialization/json/JSONTokenType.as +69 -0
  64. data/src/com/adobe/serialization/json/JSONTokenizer.as +702 -0
  65. data/src/com/adobe/utils/ArrayUtil.as +187 -0
  66. data/src/com/adobe/utils/DateUtil.as +701 -0
  67. data/src/com/adobe/utils/DictionaryUtil.as +87 -0
  68. data/src/com/adobe/utils/IntUtil.as +99 -0
  69. data/src/com/adobe/utils/NumberFormatter.as +74 -0
  70. data/src/com/adobe/utils/StringUtil.as +239 -0
  71. data/src/com/adobe/utils/XMLUtil.as +168 -0
  72. data/src/com/adobe/webapis/ServiceBase.as +48 -0
  73. data/src/com/adobe/webapis/URLLoaderBase.as +108 -0
  74. data/src/com/adobe/webapis/events/ServiceEvent.as +82 -0
  75. data/tests/src/CoreLibTestRunner-app.xml +135 -0
  76. data/tests/src/CoreLibTestRunner.as +138 -0
  77. data/tests/src/CoreLibTestRunner.mxml +46 -0
  78. data/tests/src/com/adobe/air/crypto/EncryptionKeyGeneratorTest.as +176 -0
  79. data/tests/src/com/adobe/air/filesystem/FileMonitorTest.as +63 -0
  80. data/tests/src/com/adobe/air/filesystem/VolumeMonitorTest.as +57 -0
  81. data/tests/src/com/adobe/air/filesystem/events/FileMonitorEventTest.as +59 -0
  82. data/tests/src/com/adobe/air/net/events/ResourceCacheEventTest.as +72 -0
  83. data/tests/src/com/adobe/crypto/HMACMD5Test.as +134 -0
  84. data/tests/src/com/adobe/crypto/HMACSHA1Test.as +138 -0
  85. data/tests/src/com/adobe/crypto/MD5Test.as +82 -0
  86. data/tests/src/com/adobe/crypto/SHA1Test.as +151 -0
  87. data/tests/src/com/adobe/crypto/SHA224Test.as +104 -0
  88. data/tests/src/com/adobe/crypto/SHA256Test.as +116 -0
  89. data/tests/src/com/adobe/crypto/WSSEUsernameTokenTest.as +87 -0
  90. data/tests/src/com/adobe/images/JPGEncoderTest.as +54 -0
  91. data/tests/src/com/adobe/images/PNGEncoderTest.as +54 -0
  92. data/tests/src/com/adobe/net/URITest.as +589 -0
  93. data/tests/src/com/adobe/protocols/events/ConnectedEventTest.as +59 -0
  94. data/tests/src/com/adobe/protocols/events/DatabaseEventTest.as +62 -0
  95. data/tests/src/com/adobe/protocols/events/DefinitionEventTest.as +61 -0
  96. data/tests/src/com/adobe/protocols/events/DefinitionHeaderEventTest.as +61 -0
  97. data/tests/src/com/adobe/protocols/events/DictionaryServerEventTest.as +61 -0
  98. data/tests/src/com/adobe/protocols/events/DisconnectedEventTest.as +59 -0
  99. data/tests/src/com/adobe/protocols/events/ErrorEventTest.as +63 -0
  100. data/tests/src/com/adobe/protocols/events/MatchEventTest.as +61 -0
  101. data/tests/src/com/adobe/protocols/events/MatchStrategiesEventTest.as +61 -0
  102. data/tests/src/com/adobe/protocols/events/NoMatchEventTest.as +58 -0
  103. data/tests/src/com/adobe/protocols/util/CompletedResponseEventTest.as +60 -0
  104. data/tests/src/com/adobe/serialization/json/JSONTest.as +522 -0
  105. data/tests/src/com/adobe/serialization/json/SimpleClass.as +85 -0
  106. data/tests/src/com/adobe/utils/ArrayUtilTest.as +173 -0
  107. data/tests/src/com/adobe/utils/DateUtilTest.as +436 -0
  108. data/tests/src/com/adobe/utils/DictionaryUtilTest.as +93 -0
  109. data/tests/src/com/adobe/utils/IntUtilTest.as +73 -0
  110. data/tests/src/com/adobe/utils/NumberFormatterTest.as +70 -0
  111. data/tests/src/com/adobe/utils/StringUtilTest.as +304 -0
  112. data/tests/src/com/adobe/utils/XMLUtilTest.as +101 -0
  113. data/tests/src/com/adobe/webapis/events/ServiceEventTest.as +66 -0
  114. metadata +196 -0
@@ -0,0 +1,522 @@
1
+ /*
2
+ Copyright (c) 2008, Adobe Systems Incorporated
3
+ All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are
7
+ met:
8
+
9
+ * Redistributions of source code must retain the above copyright notice,
10
+ this list of conditions and the following disclaimer.
11
+
12
+ * Redistributions in binary form must reproduce the above copyright
13
+ notice, this list of conditions and the following disclaimer in the
14
+ documentation and/or other materials provided with the distribution.
15
+
16
+ * Neither the name of Adobe Systems Incorporated nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21
+ IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+ package com.adobe.serialization.json
34
+ {
35
+ import flexunit.framework.TestCase;
36
+
37
+ public class JSONTest extends TestCase
38
+ {
39
+
40
+ public function JSONTest( methodName:String = null )
41
+ {
42
+ super( methodName );
43
+ }
44
+
45
+ /**
46
+ * Helper method to verify whether or not a given input correctly
47
+ * throws a JSONParseError
48
+ */
49
+ protected function expectParseError( jsonString:String, strict:Boolean = true ):void
50
+ {
51
+ var parseError:JSONParseError = null;
52
+
53
+ try
54
+ {
55
+ var o:* = JSON.decode( jsonString, strict );
56
+ fail( "Expecting parse error but one was not thrown" );
57
+ }
58
+ catch ( e:JSONParseError )
59
+ {
60
+ parseError = e;
61
+ }
62
+ catch ( e:Error )
63
+ {
64
+ throw e;
65
+ }
66
+
67
+ // Make sure we catch a parse error since 0xZ is an invalid number
68
+ assertNotNull( parseError );
69
+ }
70
+
71
+ /**
72
+ * Test for the JSON string true decoded to boolean true.
73
+ */
74
+ public function testDecodeTrue():void
75
+ {
76
+ var o:* = JSON.decode( "true" ) as Boolean;
77
+ assertTrue( "Expected decoded true", o );
78
+ }
79
+
80
+ public function testDecodeFalse():void
81
+ {
82
+ var o:* = JSON.decode( "false" ) as Boolean;
83
+ assertFalse( "Expected decoded false", o );
84
+ }
85
+
86
+ public function testDecodeNull():void
87
+ {
88
+ var o:* = JSON.decode( "null " );
89
+ assertNull( "Expected decoded null", o );
90
+ }
91
+
92
+ public function testDecodeString():void
93
+ {
94
+ var string:String = "this \"is\" \t a \/ string \b \f \r \h \\ \n with \' ch\\u0061rs that should be { } http://escaped.com/";
95
+ assertEquals( string, JSON.decode( JSON.encode( string ) ) );
96
+
97
+ var o:String = JSON.decode( "\"http:\/\/digg.com\/security\/Simple_Digg_Hack\"" ) as String;
98
+ assertEquals( "String not decoded correctly", "http://digg.com/security/Simple_Digg_Hack", o );
99
+
100
+ expectParseError( "\"unterminated string" );
101
+ }
102
+
103
+ public function testDecodeStringWithInvalidUnicodeEscape():void
104
+ {
105
+ // No characters after the u
106
+ expectParseError( "\"\\u\"" );
107
+
108
+ // Not a hex character after the u
109
+ expectParseError( "\"\\ut\"" );
110
+
111
+ // Not enough characters after the u
112
+ expectParseError( "\"\\u123\"" );
113
+
114
+ // Unicode decodes correctly
115
+ assertEquals( "a", JSON.decode( "\"\\u0061\"" ) );
116
+ }
117
+
118
+ // Issue #104 - http://code.google.com/p/as3corelib/issues/detail?id=104
119
+ public function testDecodeStringWithControlCharacters():void
120
+ {
121
+ var i:int;
122
+
123
+ // In strict mode, we expect an error when we try to decode a string that
124
+ // contains unescaped control characters.
125
+ for ( i = 0x00; i <= 0x1F; i++ )
126
+ {
127
+ expectParseError( "\"string with control char " + i + ": " + String.fromCharCode( i ) + "\"", true );
128
+ }
129
+
130
+ // In non-strict mode, we don't expect any errors
131
+ for ( i = 0x00; i <= 0x1F; i++ )
132
+ {
133
+ var string:String = "control char " + i + ": " + String.fromCharCode( i ) + ""
134
+ assertEquals( string,
135
+ JSON.decode( "\"" + string + "\"", false ) );
136
+ }
137
+ }
138
+
139
+ public function testDecodeZero():void
140
+ {
141
+ var n:Number = JSON.decode( "0" ) as Number;
142
+ assertEquals( n, 0 );
143
+ }
144
+
145
+ public function testDecodeZeroWithDigitsAfterIt():void
146
+ {
147
+ expectParseError( "02" );
148
+ }
149
+
150
+ public function testDecodePositiveInt():void
151
+ {
152
+ var n:int = JSON.decode( "123871" ) as int;
153
+ assertEquals( n, 123871 );
154
+ }
155
+
156
+ public function testDecodeNegativeInt():void
157
+ {
158
+ var n:int = JSON.decode( "-97123" ) as int;
159
+ assertEquals( n, -97123 );
160
+ }
161
+
162
+ public function testDecodePositiveFloat():void
163
+ {
164
+ var n:Number = JSON.decode( "12.987324" ) as Number;
165
+ assertEquals( n, 12.987324 );
166
+ }
167
+
168
+ public function testDecodeNegativeFloat():void
169
+ {
170
+ var n:Number = JSON.decode( "-1298.7324" ) as Number;
171
+ assertEquals( n, -1298.7324 );
172
+ }
173
+
174
+ public function testDecodeFloatLeadingZeroError():void
175
+ {
176
+ expectParseError( "-.2" );
177
+ }
178
+
179
+ public function testDecodeFloatDecimalMissingError():void
180
+ {
181
+ expectParseError( "1." );
182
+ }
183
+
184
+ public function testDecodeScientificRegularExponent():void
185
+ {
186
+ var n:Number = JSON.decode( "6.02e2" ) as Number;
187
+ assertEquals( n, 602 );
188
+
189
+ n = JSON.decode( "-2e10" );
190
+ assertEquals( n, -20000000000 );
191
+ assertEquals( n, -2 * Math.pow( 10, 10 ) );
192
+ }
193
+
194
+ public function testDecodeScientificPositiveExponent():void
195
+ {
196
+ var n:Number = JSON.decode( "2E+9" ) as Number;
197
+ assertEquals( n, 2 * Math.pow( 10, 9 ) );
198
+
199
+ n = JSON.decode( "-2.2E+23" ) as Number;
200
+ assertEquals( n, -2.2 * Math.pow( 10, 23 ) );
201
+ }
202
+
203
+ public function testDecodeScientificNegativeExponent():void
204
+ {
205
+ var n:Number = JSON.decode( "6.02e-23" ) as Number;
206
+ assertEquals( n, 6.02 * Math.pow( 10, -23 ) );
207
+
208
+ n = JSON.decode( "-4e-9" ) as Number;
209
+ assertEquals( n, -4 * Math.pow( 10, -9 ) );
210
+
211
+ n = JSON.decode( "0E-2" ) as Number;
212
+ assertEquals( n, 0 );
213
+ }
214
+
215
+ public function testDecodeScientificExponentError():void
216
+ {
217
+ expectParseError( "1e" );
218
+ }
219
+
220
+ /**
221
+ * In non-strict mode, we can interpret hex values as numbers
222
+ */
223
+ public function testDecodeHex():void
224
+ {
225
+ var n:Number = JSON.decode( "0xFF0033", false );
226
+ assertEquals( 0xFF0033, n );
227
+
228
+ // Verify invalid hex format throws error
229
+ expectParseError( "0x", false );
230
+
231
+ // Verify invalid hex format throws error
232
+ expectParseError( "0xZ", false );
233
+ expectParseError( "0xz", false );
234
+
235
+ // Verify that strict mode errors
236
+ expectParseError( "0xFF0033" );
237
+ }
238
+
239
+ /**
240
+ * Non-strict mode allows for NaN as a valid token
241
+ */
242
+ public function testDecodeNaN():void
243
+ {
244
+ var n:Number = JSON.decode( "NaN", false ) as Number;
245
+ assertTrue( isNaN( n ) );
246
+
247
+ var o:Object = JSON.decode( "{ \"num\": NaN }", false );
248
+ assertNotNull( o );
249
+ assertTrue( isNaN( o.num ) );
250
+
251
+ // Verify that strict mode throws an error
252
+ expectParseError( "NaN" );
253
+ expectParseError( "{ \"num\": NaN }" );
254
+ }
255
+
256
+ public function testDecodeObject():void
257
+ {
258
+ var o:* = JSON.decode( " { \"test\": true, \"test2\": -12356732.12 } " ) as Object;
259
+ assertNotNull( o );
260
+
261
+ assertEquals( "Expected decoded object.test = true", true, o.test );
262
+ assertEquals( "Expected decoded object.test2 = -12356732.12", -12356732.12, o.test2 );
263
+ }
264
+
265
+ /**
266
+ * In non-strict mode, the object can have a trailing comma after
267
+ * the last member and not throw an error.
268
+ */
269
+ public function testDecodeObjectWithTrailingComma():void
270
+ {
271
+ var o:Object = JSON.decode( "{\"p1\":true,\"p2\":false,}", false );
272
+ assertNotNull( o );
273
+ assertTrue( o.p1 );
274
+ assertFalse( o.p2 );
275
+
276
+ o = JSON.decode( "{,}", false );
277
+ assertNotNull( o );
278
+
279
+ // Verify strict mode throws error with trailing comma
280
+ expectParseError( "{\"p1\":true,\"p2\":false,}" );
281
+ expectParseError( "{,}" );
282
+ }
283
+
284
+ /**
285
+ * Leading comma is not supported (yet? should they be?)
286
+ */
287
+ public function testDecodeObjectWithLeadingCommaFails():void
288
+ {
289
+ expectParseError( "[,\"p1\":true}", false );
290
+ }
291
+
292
+ public function testDecodeArray():void
293
+ {
294
+ var o:* = JSON.decode( " [ null, true, false, 100, -100, \"test\", { \"foo\": \"bar\" } ] " ) as Array;
295
+ assertNull( "Expected decoded array[0] == null", o[0] );
296
+ assertTrue( "Expected decoded array[1] == true", o[1] );
297
+ assertFalse( "Expected decoded array[2] == false", o[2] );
298
+ assertEquals( "Expected decoded array[3] == 100", 100, o[3] );
299
+ assertEquals( "Expected decoded array[4] == -100", -100, o[4] );
300
+ assertEquals( "Expected decoded array[5] == \"test\"", "test", o[5] );
301
+ assertEquals( "Expected decoded array[6].foo == \"bar\"", "bar", o[6].foo );
302
+ }
303
+
304
+ public function testDecodeArrayWithNewlines():void
305
+ {
306
+ var o:Array = JSON.decode( "\n [ \nnull, \n \r \t \r true \n ] \r \t \r \n" ) as Array;
307
+
308
+ assertNull( "Expected decoded with newlines array[0] == null", o[0] );
309
+ assertTrue( "Expected decoded with newlines array[1] == true", o[1] );
310
+ }
311
+
312
+ /**
313
+ * In non-strict mode, the array can have a trailing comma after
314
+ * the last element and not throw an error.
315
+ */
316
+ public function testDecodeArrayWithTrailingComma():void
317
+ {
318
+ var o:Array = JSON.decode( "[0,1,]", false ) as Array;
319
+ assertEquals( 0, o[0] );
320
+ assertEquals( 1, o[1] );
321
+
322
+ o = JSON.decode( "[,]", false ) as Array;
323
+ assertNotNull( o );
324
+ assertEquals( 0, o.length );
325
+
326
+ // Verify strict mode throws error with trailing comma
327
+ expectParseError( "[0,1,]" );
328
+ expectParseError( "[,]" );
329
+ }
330
+
331
+ /**
332
+ * Leading comma is not supported (yet? should they be?)
333
+ */
334
+ public function testDecodeArrayWithLeadingCommaFails():void
335
+ {
336
+ expectParseError( "[,10]", false );
337
+ }
338
+
339
+ public function testDecodeComments():void
340
+ {
341
+ expectParseError( "/*" );
342
+ expectParseError( "/*/" );
343
+ expectParseError( "//" );
344
+
345
+ var n:Number = JSON.decode( "// this is a number\n12" );
346
+ assertEquals( 12, n );
347
+
348
+ n = JSON.decode( "/* \n\n multiine com//ment *///\n14" );
349
+ assertEquals( 14, n )
350
+ }
351
+
352
+ public function testDecodeSuccessiveComments():void
353
+ {
354
+ var jsonString:String = " // test comment"
355
+ + "\n // test comment line 2"
356
+ + "\nfalse";
357
+ var o:* = JSON.decode( jsonString );
358
+ assertEquals( false, o );
359
+ }
360
+
361
+ public function testDecodeEmptyStringError():void
362
+ {
363
+ expectParseError( "" );
364
+ }
365
+
366
+ public function testDecodeWhiteSpace():void
367
+ {
368
+ var n:Number;
369
+ var nbsp:String = String.fromCharCode( 160 ); // non-breaking space
370
+
371
+ n = JSON.decode( " 1 " );
372
+ assertEquals( 1, n );
373
+
374
+ n = JSON.decode( "\t2\t" );
375
+ assertEquals( 2, n );
376
+
377
+ n = JSON.decode( "\r3\r" );
378
+ assertEquals( 3, n );
379
+
380
+ n = JSON.decode( "\n4\n" );
381
+ assertEquals( 4, n );
382
+
383
+ // Verify combined before/after spacing
384
+ n = JSON.decode( "\t \n\n\r \r\n\t 100 \r\n\t\r\r\r\n \n" ) as Number
385
+ assertEquals( 100, n );
386
+
387
+ // In non-strict mode, we should also accept non breaking space
388
+ n = JSON.decode( "\t \n"
389
+ + nbsp
390
+ + "\n\r \r\n\t 100 \r\n\t\r\r\r\n"
391
+ + nbsp
392
+ + " \n", false ) as Number
393
+ assertEquals( 100, n );
394
+
395
+ // In strict mode, we do NOT accept non breaking space, so expect a parse error
396
+ expectParseError( "\t \n" + nbsp + "\n\r \r\n\t 100 \r\n\t\r\r\r\n" + nbsp + " \n" );
397
+ }
398
+
399
+ public function testDecodeWithCharactersLeftInInputString():void
400
+ {
401
+ // strict mode throws errors
402
+ expectParseError( "[ 1 ] true" );
403
+ expectParseError( "0xZ" );
404
+ expectParseError( "true Z" );
405
+
406
+ // non-strict mode will not throw errors
407
+ var a:Array = JSON.decode( "[ 1 ] true", false ) as Array;
408
+ assertEquals( 1, a[0] );
409
+
410
+ var n:Number = JSON.decode( "1Z", false ) as Number;
411
+ assertEquals( 1, n );
412
+
413
+ var b:Boolean = JSON.decode( "true Z", false ) as Boolean;
414
+ assertTrue( b );
415
+ }
416
+
417
+ public function testEncodeTrue():void
418
+ {
419
+ var o:String = JSON.encode( true );
420
+ assertEquals( "Expected encoded true", "true", o );
421
+ }
422
+
423
+ public function testEncodeFalse():void
424
+ {
425
+ var o:String = JSON.encode( false );
426
+ assertEquals( "Expected encoded false", "false", o );
427
+ }
428
+
429
+ public function testEncodeNull():void
430
+ {
431
+ var o:String = JSON.encode( null );
432
+ assertEquals( "Expected encoded null", "null", o );
433
+ }
434
+
435
+ public function testEncodeString():void
436
+ {
437
+ var o:String = JSON.encode( "this is a \n \"string\"" );
438
+ assertEquals( "Expected encoded string", "\"this is a \\n \\\"string\\\"\"", o );
439
+
440
+ o = JSON.encode( "myString" );
441
+ assertEquals( "\"myString\"", o );
442
+ }
443
+
444
+ public function testEncodeArrayEmpty():void
445
+ {
446
+ var o:String = JSON.encode( [] );
447
+ assertEquals( "Expected encoded []", "[]", o );
448
+ }
449
+
450
+ public function testEncodeArray():void
451
+ {
452
+ var o:String = JSON.encode( [ true, false, -10, null, Number.NEGATIVE_INFINITY ] );
453
+ assertTrue( "Expected encoded array", "[true,false,-10,null,null]", o );
454
+ }
455
+
456
+ public function testEncodeObjectEmpty():void
457
+ {
458
+ var o:String = JSON.encode( {} );
459
+ assertTrue( "Expected encoded {}", "{}", o );
460
+ }
461
+
462
+ public function testEncodeObject():void
463
+ {
464
+ // Note: because order cannot be guaranteed when decoding
465
+ // into a string, we can't reliably test an object with
466
+ // multiple properties, so instead we test multiple
467
+ // smaller objects
468
+ //var obj:Object = new Object();
469
+ //obj.test1 = true;
470
+ //obj["test 2"] = false;
471
+ //obj[" test _3" ] = { foo: "bar" };
472
+
473
+ //var o:String = JSON.encode( obj );
474
+ //assertTrue( "Expected encoded object", o == "{\"test1\":true,\"test 2\":false,\" test _3\":{\"foo\":\"bar\"}}" );
475
+
476
+ var obj:Object = { foo: { foo2: { foo3: { foo4: "bar" } } } };
477
+ var s:String = JSON.encode( obj );
478
+ assertEquals( "Deeply nested", "{\"foo\":{\"foo2\":{\"foo3\":{\"foo4\":\"bar\"}}}}", s );
479
+
480
+ obj = new Object();
481
+ obj[" prop with spaces "] = true;
482
+ s = JSON.encode( obj );
483
+ assertEquals( "Prop with spaces", "{\" prop with spaces \":true}", s );
484
+ }
485
+
486
+ public function testEncodeClassInstance():void
487
+ {
488
+ var customObject:SimpleClass = new SimpleClass();
489
+ customObject.transientVar = "Should not be encoded";
490
+
491
+ var s:String = JSON.encode( customObject );
492
+
493
+ assertTrue( "Has length", s.length > 0 );
494
+ // Make sure the transient variable was not encoded
495
+ assertEquals( "Should not find transient var in string", -1, s.indexOf( "\"transientVar\":\"Should not be encoded\"" ) );
496
+
497
+ // Decode the string so we can verify that it has the properties
498
+ var o:Object = JSON.decode( s );
499
+
500
+ assertNotNull( o );
501
+ assertNotNull( o.publicVar1 );
502
+ assertNotNull( o.publicVar2 );
503
+ assertNotNull( o.accessor1 );
504
+ assertNotNull( o.accessor2 );
505
+ assertNull( o.transientVar );
506
+ assertEquals( 17, o.publicVar1 );
507
+ assertEquals( 20, o.publicVar2 );
508
+ assertEquals( 25, o.accessor1 );
509
+ assertEquals( 30, o.accessor2 );
510
+
511
+ // Make sure o only has 4 properties
512
+ var count:int = 0;
513
+ for ( var key:String in o )
514
+ {
515
+ count++;
516
+ }
517
+ assertEquals( 4, count );
518
+ }
519
+
520
+ }
521
+
522
+ }
@@ -0,0 +1,85 @@
1
+ /*
2
+ Copyright (c) 2008, Adobe Systems Incorporated
3
+ All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are
7
+ met:
8
+
9
+ * Redistributions of source code must retain the above copyright notice,
10
+ this list of conditions and the following disclaimer.
11
+
12
+ * Redistributions in binary form must reproduce the above copyright
13
+ notice, this list of conditions and the following disclaimer in the
14
+ documentation and/or other materials provided with the distribution.
15
+
16
+ * Neither the name of Adobe Systems Incorporated nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21
+ IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+ package com.adobe.serialization.json
34
+ {
35
+
36
+ /**
37
+ * This is just a simple class with some properties
38
+ * so that we can test converting the class instance
39
+ * to a JSON string.
40
+ */
41
+ public class SimpleClass
42
+ {
43
+
44
+ public var publicVar1:int = 17;
45
+ public var publicVar2:int = 20;
46
+
47
+ protected var protectedVar:int = 0;
48
+
49
+ private var privateVar:int = -17;
50
+
51
+ public function get accessor1():int
52
+ {
53
+ return 25;
54
+ }
55
+
56
+ public function get accessor2():int
57
+ {
58
+ return 30;
59
+ }
60
+
61
+ /**
62
+ * Constructor
63
+ */
64
+ public function SimpleClass()
65
+ {
66
+
67
+ }
68
+
69
+ public function method():Boolean
70
+ {
71
+ return false;
72
+ }
73
+
74
+ [Transient]
75
+ public var transientVar:String;
76
+
77
+ /** Write-only property */
78
+ public function set writeOnlyProp( value:Number ):void
79
+ {
80
+
81
+ }
82
+
83
+
84
+ }
85
+ }