as3corelib 0.1.0 → 0.85.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -4
- data/Gemfile.lock +7 -4
- data/README.textile +20 -10
- data/Rakefile +32 -2
- data/as3corelib.gemspec +13 -11
- data/lib/as3corelib.rb +10 -4
- data/lib/as3corelib/version.rb +4 -0
- metadata +31 -121
- data/build/build.properties +0 -48
- data/build/build.xml +0 -104
- data/examples/JSONExample/JSONExample.mxml +0 -73
- data/src/com/adobe/air/crypto/EncryptionKeyGenerator.as +0 -313
- data/src/com/adobe/air/filesystem/FileMonitor.as +0 -245
- data/src/com/adobe/air/filesystem/FileUtil.as +0 -63
- data/src/com/adobe/air/filesystem/VolumeMonitor.as +0 -184
- data/src/com/adobe/air/filesystem/events/FileMonitorEvent.as +0 -61
- data/src/com/adobe/air/logging/FileTarget.as +0 -95
- data/src/com/adobe/air/net/ResourceCache.as +0 -165
- data/src/com/adobe/air/net/events/ResourceCacheEvent.as +0 -70
- data/src/com/adobe/crypto/HMAC.as +0 -127
- data/src/com/adobe/crypto/MD5.as +0 -281
- data/src/com/adobe/crypto/MD5Stream.as +0 -402
- data/src/com/adobe/crypto/SHA1.as +0 -289
- data/src/com/adobe/crypto/SHA224.as +0 -257
- data/src/com/adobe/crypto/SHA256.as +0 -261
- data/src/com/adobe/crypto/WSSEUsernameToken.as +0 -114
- data/src/com/adobe/errors/IllegalStateError.as +0 -63
- data/src/com/adobe/fileformats/vcard/Address.as +0 -47
- data/src/com/adobe/fileformats/vcard/Email.as +0 -39
- data/src/com/adobe/fileformats/vcard/Phone.as +0 -39
- data/src/com/adobe/fileformats/vcard/VCard.as +0 -54
- data/src/com/adobe/fileformats/vcard/VCardParser.as +0 -246
- data/src/com/adobe/images/BitString.as +0 -39
- data/src/com/adobe/images/JPGEncoder.as +0 -648
- data/src/com/adobe/images/PNGEncoder.as +0 -141
- data/src/com/adobe/net/DynamicURLLoader.as +0 -55
- data/src/com/adobe/net/IURIResolver.as +0 -76
- data/src/com/adobe/net/MimeTypeMap.as +0 -200
- data/src/com/adobe/net/URI.as +0 -2466
- data/src/com/adobe/net/URIEncodingBitmap.as +0 -139
- data/src/com/adobe/net/proxies/RFC2817Socket.as +0 -198
- data/src/com/adobe/protocols/dict/Database.as +0 -66
- data/src/com/adobe/protocols/dict/Definition.as +0 -71
- data/src/com/adobe/protocols/dict/Dict.as +0 -360
- data/src/com/adobe/protocols/dict/DictionaryServer.as +0 -60
- data/src/com/adobe/protocols/dict/MatchStrategy.as +0 -66
- data/src/com/adobe/protocols/dict/Response.as +0 -71
- data/src/com/adobe/protocols/dict/events/ConnectedEvent.as +0 -53
- data/src/com/adobe/protocols/dict/events/DatabaseEvent.as +0 -67
- data/src/com/adobe/protocols/dict/events/DefinitionEvent.as +0 -70
- data/src/com/adobe/protocols/dict/events/DefinitionHeaderEvent.as +0 -69
- data/src/com/adobe/protocols/dict/events/DictionaryServerEvent.as +0 -69
- data/src/com/adobe/protocols/dict/events/DisconnectedEvent.as +0 -55
- data/src/com/adobe/protocols/dict/events/ErrorEvent.as +0 -80
- data/src/com/adobe/protocols/dict/events/MatchEvent.as +0 -67
- data/src/com/adobe/protocols/dict/events/MatchStrategiesEvent.as +0 -70
- data/src/com/adobe/protocols/dict/events/NoMatchEvent.as +0 -54
- data/src/com/adobe/protocols/dict/util/CompleteResponseEvent.as +0 -68
- data/src/com/adobe/protocols/dict/util/SocketHelper.as +0 -81
- data/src/com/adobe/serialization/json/JSON.as +0 -86
- data/src/com/adobe/serialization/json/JSONDecoder.as +0 -327
- data/src/com/adobe/serialization/json/JSONEncoder.as +0 -312
- data/src/com/adobe/serialization/json/JSONParseError.as +0 -87
- data/src/com/adobe/serialization/json/JSONToken.as +0 -104
- data/src/com/adobe/serialization/json/JSONTokenType.as +0 -69
- data/src/com/adobe/serialization/json/JSONTokenizer.as +0 -702
- data/src/com/adobe/utils/ArrayUtil.as +0 -187
- data/src/com/adobe/utils/DateUtil.as +0 -701
- data/src/com/adobe/utils/DictionaryUtil.as +0 -87
- data/src/com/adobe/utils/IntUtil.as +0 -99
- data/src/com/adobe/utils/NumberFormatter.as +0 -74
- data/src/com/adobe/utils/StringUtil.as +0 -239
- data/src/com/adobe/utils/XMLUtil.as +0 -168
- data/src/com/adobe/webapis/ServiceBase.as +0 -48
- data/src/com/adobe/webapis/URLLoaderBase.as +0 -108
- data/src/com/adobe/webapis/events/ServiceEvent.as +0 -82
- data/tests/src/CoreLibTestRunner-app.xml +0 -135
- data/tests/src/CoreLibTestRunner.as +0 -138
- data/tests/src/CoreLibTestRunner.mxml +0 -46
- data/tests/src/com/adobe/air/crypto/EncryptionKeyGeneratorTest.as +0 -176
- data/tests/src/com/adobe/air/filesystem/FileMonitorTest.as +0 -63
- data/tests/src/com/adobe/air/filesystem/VolumeMonitorTest.as +0 -57
- data/tests/src/com/adobe/air/filesystem/events/FileMonitorEventTest.as +0 -59
- data/tests/src/com/adobe/air/net/events/ResourceCacheEventTest.as +0 -72
- data/tests/src/com/adobe/crypto/HMACMD5Test.as +0 -134
- data/tests/src/com/adobe/crypto/HMACSHA1Test.as +0 -138
- data/tests/src/com/adobe/crypto/MD5Test.as +0 -82
- data/tests/src/com/adobe/crypto/SHA1Test.as +0 -151
- data/tests/src/com/adobe/crypto/SHA224Test.as +0 -104
- data/tests/src/com/adobe/crypto/SHA256Test.as +0 -116
- data/tests/src/com/adobe/crypto/WSSEUsernameTokenTest.as +0 -87
- data/tests/src/com/adobe/images/JPGEncoderTest.as +0 -54
- data/tests/src/com/adobe/images/PNGEncoderTest.as +0 -54
- data/tests/src/com/adobe/net/URITest.as +0 -589
- data/tests/src/com/adobe/protocols/events/ConnectedEventTest.as +0 -59
- data/tests/src/com/adobe/protocols/events/DatabaseEventTest.as +0 -62
- data/tests/src/com/adobe/protocols/events/DefinitionEventTest.as +0 -61
- data/tests/src/com/adobe/protocols/events/DefinitionHeaderEventTest.as +0 -61
- data/tests/src/com/adobe/protocols/events/DictionaryServerEventTest.as +0 -61
- data/tests/src/com/adobe/protocols/events/DisconnectedEventTest.as +0 -59
- data/tests/src/com/adobe/protocols/events/ErrorEventTest.as +0 -63
- data/tests/src/com/adobe/protocols/events/MatchEventTest.as +0 -61
- data/tests/src/com/adobe/protocols/events/MatchStrategiesEventTest.as +0 -61
- data/tests/src/com/adobe/protocols/events/NoMatchEventTest.as +0 -58
- data/tests/src/com/adobe/protocols/util/CompletedResponseEventTest.as +0 -60
- data/tests/src/com/adobe/serialization/json/JSONTest.as +0 -522
- data/tests/src/com/adobe/serialization/json/SimpleClass.as +0 -85
- data/tests/src/com/adobe/utils/ArrayUtilTest.as +0 -173
- data/tests/src/com/adobe/utils/DateUtilTest.as +0 -436
- data/tests/src/com/adobe/utils/DictionaryUtilTest.as +0 -93
- data/tests/src/com/adobe/utils/IntUtilTest.as +0 -73
- data/tests/src/com/adobe/utils/NumberFormatterTest.as +0 -70
- data/tests/src/com/adobe/utils/StringUtilTest.as +0 -304
- data/tests/src/com/adobe/utils/XMLUtilTest.as +0 -101
- data/tests/src/com/adobe/webapis/events/ServiceEventTest.as +0 -66
@@ -1,327 +0,0 @@
|
|
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
|
-
public class JSONDecoder
|
37
|
-
{
|
38
|
-
|
39
|
-
/**
|
40
|
-
* Flag indicating if the parser should be strict about the format
|
41
|
-
* of the JSON string it is attempting to decode.
|
42
|
-
*/
|
43
|
-
private var strict:Boolean;
|
44
|
-
|
45
|
-
/** The value that will get parsed from the JSON string */
|
46
|
-
private var value:*;
|
47
|
-
|
48
|
-
/** The tokenizer designated to read the JSON string */
|
49
|
-
private var tokenizer:JSONTokenizer;
|
50
|
-
|
51
|
-
/** The current token from the tokenizer */
|
52
|
-
private var token:JSONToken;
|
53
|
-
|
54
|
-
/**
|
55
|
-
* Constructs a new JSONDecoder to parse a JSON string
|
56
|
-
* into a native object.
|
57
|
-
*
|
58
|
-
* @param s The JSON string to be converted
|
59
|
-
* into a native object
|
60
|
-
* @param strict Flag indicating if the JSON string needs to
|
61
|
-
* strictly match the JSON standard or not.
|
62
|
-
* @langversion ActionScript 3.0
|
63
|
-
* @playerversion Flash 9.0
|
64
|
-
* @tiptext
|
65
|
-
*/
|
66
|
-
public function JSONDecoder( s:String, strict:Boolean )
|
67
|
-
{
|
68
|
-
this.strict = strict;
|
69
|
-
tokenizer = new JSONTokenizer( s, strict );
|
70
|
-
|
71
|
-
nextToken();
|
72
|
-
value = parseValue();
|
73
|
-
|
74
|
-
// Make sure the input stream is empty
|
75
|
-
if ( strict && nextToken() != null )
|
76
|
-
{
|
77
|
-
tokenizer.parseError( "Unexpected characters left in input stream" );
|
78
|
-
}
|
79
|
-
}
|
80
|
-
|
81
|
-
/**
|
82
|
-
* Gets the internal object that was created by parsing
|
83
|
-
* the JSON string passed to the constructor.
|
84
|
-
*
|
85
|
-
* @return The internal object representation of the JSON
|
86
|
-
* string that was passed to the constructor
|
87
|
-
* @langversion ActionScript 3.0
|
88
|
-
* @playerversion Flash 9.0
|
89
|
-
* @tiptext
|
90
|
-
*/
|
91
|
-
public function getValue():*
|
92
|
-
{
|
93
|
-
return value;
|
94
|
-
}
|
95
|
-
|
96
|
-
/**
|
97
|
-
* Returns the next token from the tokenzier reading
|
98
|
-
* the JSON string
|
99
|
-
*/
|
100
|
-
private function nextToken():JSONToken
|
101
|
-
{
|
102
|
-
return token = tokenizer.getNextToken();
|
103
|
-
}
|
104
|
-
|
105
|
-
/**
|
106
|
-
* Attempt to parse an array.
|
107
|
-
*/
|
108
|
-
private function parseArray():Array
|
109
|
-
{
|
110
|
-
// create an array internally that we're going to attempt
|
111
|
-
// to parse from the tokenizer
|
112
|
-
var a:Array = new Array();
|
113
|
-
|
114
|
-
// grab the next token from the tokenizer to move
|
115
|
-
// past the opening [
|
116
|
-
nextToken();
|
117
|
-
|
118
|
-
// check to see if we have an empty array
|
119
|
-
if ( token.type == JSONTokenType.RIGHT_BRACKET )
|
120
|
-
{
|
121
|
-
// we're done reading the array, so return it
|
122
|
-
return a;
|
123
|
-
}
|
124
|
-
// in non-strict mode an empty array is also a comma
|
125
|
-
// followed by a right bracket
|
126
|
-
else if ( !strict && token.type == JSONTokenType.COMMA )
|
127
|
-
{
|
128
|
-
// move past the comma
|
129
|
-
nextToken();
|
130
|
-
|
131
|
-
// check to see if we're reached the end of the array
|
132
|
-
if ( token.type == JSONTokenType.RIGHT_BRACKET )
|
133
|
-
{
|
134
|
-
return a;
|
135
|
-
}
|
136
|
-
else
|
137
|
-
{
|
138
|
-
tokenizer.parseError( "Leading commas are not supported. Expecting ']' but found " + token.value );
|
139
|
-
}
|
140
|
-
}
|
141
|
-
|
142
|
-
// deal with elements of the array, and use an "infinite"
|
143
|
-
// loop because we could have any amount of elements
|
144
|
-
while ( true )
|
145
|
-
{
|
146
|
-
// read in the value and add it to the array
|
147
|
-
a.push( parseValue() );
|
148
|
-
|
149
|
-
// after the value there should be a ] or a ,
|
150
|
-
nextToken();
|
151
|
-
|
152
|
-
if ( token.type == JSONTokenType.RIGHT_BRACKET )
|
153
|
-
{
|
154
|
-
// we're done reading the array, so return it
|
155
|
-
return a;
|
156
|
-
}
|
157
|
-
else if ( token.type == JSONTokenType.COMMA )
|
158
|
-
{
|
159
|
-
// move past the comma and read another value
|
160
|
-
nextToken();
|
161
|
-
|
162
|
-
// Allow arrays to have a comma after the last element
|
163
|
-
// if the decoder is not in strict mode
|
164
|
-
if ( !strict )
|
165
|
-
{
|
166
|
-
// Reached ",]" as the end of the array, so return it
|
167
|
-
if ( token.type == JSONTokenType.RIGHT_BRACKET )
|
168
|
-
{
|
169
|
-
return a;
|
170
|
-
}
|
171
|
-
}
|
172
|
-
}
|
173
|
-
else
|
174
|
-
{
|
175
|
-
tokenizer.parseError( "Expecting ] or , but found " + token.value );
|
176
|
-
}
|
177
|
-
}
|
178
|
-
return null;
|
179
|
-
}
|
180
|
-
|
181
|
-
/**
|
182
|
-
* Attempt to parse an object.
|
183
|
-
*/
|
184
|
-
private function parseObject():Object
|
185
|
-
{
|
186
|
-
// create the object internally that we're going to
|
187
|
-
// attempt to parse from the tokenizer
|
188
|
-
var o:Object = new Object();
|
189
|
-
|
190
|
-
// store the string part of an object member so
|
191
|
-
// that we can assign it a value in the object
|
192
|
-
var key:String
|
193
|
-
|
194
|
-
// grab the next token from the tokenizer
|
195
|
-
nextToken();
|
196
|
-
|
197
|
-
// check to see if we have an empty object
|
198
|
-
if ( token.type == JSONTokenType.RIGHT_BRACE )
|
199
|
-
{
|
200
|
-
// we're done reading the object, so return it
|
201
|
-
return o;
|
202
|
-
}
|
203
|
-
// in non-strict mode an empty object is also a comma
|
204
|
-
// followed by a right bracket
|
205
|
-
else if ( !strict && token.type == JSONTokenType.COMMA )
|
206
|
-
{
|
207
|
-
// move past the comma
|
208
|
-
nextToken();
|
209
|
-
|
210
|
-
// check to see if we're reached the end of the object
|
211
|
-
if ( token.type == JSONTokenType.RIGHT_BRACE )
|
212
|
-
{
|
213
|
-
return o;
|
214
|
-
}
|
215
|
-
else
|
216
|
-
{
|
217
|
-
tokenizer.parseError( "Leading commas are not supported. Expecting '}' but found " + token.value );
|
218
|
-
}
|
219
|
-
}
|
220
|
-
|
221
|
-
// deal with members of the object, and use an "infinite"
|
222
|
-
// loop because we could have any amount of members
|
223
|
-
while ( true )
|
224
|
-
{
|
225
|
-
if ( token.type == JSONTokenType.STRING )
|
226
|
-
{
|
227
|
-
// the string value we read is the key for the object
|
228
|
-
key = String( token.value );
|
229
|
-
|
230
|
-
// move past the string to see what's next
|
231
|
-
nextToken();
|
232
|
-
|
233
|
-
// after the string there should be a :
|
234
|
-
if ( token.type == JSONTokenType.COLON )
|
235
|
-
{
|
236
|
-
// move past the : and read/assign a value for the key
|
237
|
-
nextToken();
|
238
|
-
o[key] = parseValue();
|
239
|
-
|
240
|
-
// move past the value to see what's next
|
241
|
-
nextToken();
|
242
|
-
|
243
|
-
// after the value there's either a } or a ,
|
244
|
-
if ( token.type == JSONTokenType.RIGHT_BRACE )
|
245
|
-
{
|
246
|
-
// we're done reading the object, so return it
|
247
|
-
return o;
|
248
|
-
}
|
249
|
-
else if ( token.type == JSONTokenType.COMMA )
|
250
|
-
{
|
251
|
-
// skip past the comma and read another member
|
252
|
-
nextToken();
|
253
|
-
|
254
|
-
// Allow objects to have a comma after the last member
|
255
|
-
// if the decoder is not in strict mode
|
256
|
-
if ( !strict )
|
257
|
-
{
|
258
|
-
// Reached ",}" as the end of the object, so return it
|
259
|
-
if ( token.type == JSONTokenType.RIGHT_BRACE )
|
260
|
-
{
|
261
|
-
return o;
|
262
|
-
}
|
263
|
-
}
|
264
|
-
}
|
265
|
-
else
|
266
|
-
{
|
267
|
-
tokenizer.parseError( "Expecting } or , but found " + token.value );
|
268
|
-
}
|
269
|
-
}
|
270
|
-
else
|
271
|
-
{
|
272
|
-
tokenizer.parseError( "Expecting : but found " + token.value );
|
273
|
-
}
|
274
|
-
}
|
275
|
-
else
|
276
|
-
{
|
277
|
-
tokenizer.parseError( "Expecting string but found " + token.value );
|
278
|
-
}
|
279
|
-
}
|
280
|
-
return null;
|
281
|
-
}
|
282
|
-
|
283
|
-
/**
|
284
|
-
* Attempt to parse a value
|
285
|
-
*/
|
286
|
-
private function parseValue():Object
|
287
|
-
{
|
288
|
-
// Catch errors when the input stream ends abruptly
|
289
|
-
if ( token == null )
|
290
|
-
{
|
291
|
-
tokenizer.parseError( "Unexpected end of input" );
|
292
|
-
}
|
293
|
-
|
294
|
-
switch ( token.type )
|
295
|
-
{
|
296
|
-
case JSONTokenType.LEFT_BRACE:
|
297
|
-
return parseObject();
|
298
|
-
|
299
|
-
case JSONTokenType.LEFT_BRACKET:
|
300
|
-
return parseArray();
|
301
|
-
|
302
|
-
case JSONTokenType.STRING:
|
303
|
-
case JSONTokenType.NUMBER:
|
304
|
-
case JSONTokenType.TRUE:
|
305
|
-
case JSONTokenType.FALSE:
|
306
|
-
case JSONTokenType.NULL:
|
307
|
-
return token.value;
|
308
|
-
|
309
|
-
case JSONTokenType.NAN:
|
310
|
-
if ( !strict )
|
311
|
-
{
|
312
|
-
return token.value;
|
313
|
-
}
|
314
|
-
else
|
315
|
-
{
|
316
|
-
tokenizer.parseError( "Unexpected " + token.value );
|
317
|
-
}
|
318
|
-
|
319
|
-
default:
|
320
|
-
tokenizer.parseError( "Unexpected " + token.value );
|
321
|
-
|
322
|
-
}
|
323
|
-
|
324
|
-
return null;
|
325
|
-
}
|
326
|
-
}
|
327
|
-
}
|
@@ -1,312 +0,0 @@
|
|
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
|
-
import flash.utils.describeType;
|
37
|
-
|
38
|
-
public class JSONEncoder {
|
39
|
-
|
40
|
-
/** The string that is going to represent the object we're encoding */
|
41
|
-
private var jsonString:String;
|
42
|
-
|
43
|
-
/**
|
44
|
-
* Creates a new JSONEncoder.
|
45
|
-
*
|
46
|
-
* @param o The object to encode as a JSON string
|
47
|
-
* @langversion ActionScript 3.0
|
48
|
-
* @playerversion Flash 9.0
|
49
|
-
* @tiptext
|
50
|
-
*/
|
51
|
-
public function JSONEncoder( value:* ) {
|
52
|
-
jsonString = convertToString( value );
|
53
|
-
|
54
|
-
}
|
55
|
-
|
56
|
-
/**
|
57
|
-
* Gets the JSON string from the encoder.
|
58
|
-
*
|
59
|
-
* @return The JSON string representation of the object
|
60
|
-
* that was passed to the constructor
|
61
|
-
* @langversion ActionScript 3.0
|
62
|
-
* @playerversion Flash 9.0
|
63
|
-
* @tiptext
|
64
|
-
*/
|
65
|
-
public function getString():String {
|
66
|
-
return jsonString;
|
67
|
-
}
|
68
|
-
|
69
|
-
/**
|
70
|
-
* Converts a value to it's JSON string equivalent.
|
71
|
-
*
|
72
|
-
* @param value The value to convert. Could be any
|
73
|
-
* type (object, number, array, etc)
|
74
|
-
*/
|
75
|
-
private function convertToString( value:* ):String {
|
76
|
-
|
77
|
-
// determine what value is and convert it based on it's type
|
78
|
-
if ( value is String ) {
|
79
|
-
|
80
|
-
// escape the string so it's formatted correctly
|
81
|
-
return escapeString( value as String );
|
82
|
-
|
83
|
-
} else if ( value is Number ) {
|
84
|
-
|
85
|
-
// only encode numbers that finate
|
86
|
-
return isFinite( value as Number) ? value.toString() : "null";
|
87
|
-
|
88
|
-
} else if ( value is Boolean ) {
|
89
|
-
|
90
|
-
// convert boolean to string easily
|
91
|
-
return value ? "true" : "false";
|
92
|
-
|
93
|
-
} else if ( value is Array ) {
|
94
|
-
|
95
|
-
// call the helper method to convert an array
|
96
|
-
return arrayToString( value as Array );
|
97
|
-
|
98
|
-
} else if ( value is Object && value != null ) {
|
99
|
-
|
100
|
-
// call the helper method to convert an object
|
101
|
-
return objectToString( value );
|
102
|
-
}
|
103
|
-
return "null";
|
104
|
-
}
|
105
|
-
|
106
|
-
/**
|
107
|
-
* Escapes a string accoding to the JSON specification.
|
108
|
-
*
|
109
|
-
* @param str The string to be escaped
|
110
|
-
* @return The string with escaped special characters
|
111
|
-
* according to the JSON specification
|
112
|
-
*/
|
113
|
-
private function escapeString( str:String ):String {
|
114
|
-
// create a string to store the string's jsonstring value
|
115
|
-
var s:String = "";
|
116
|
-
// current character in the string we're processing
|
117
|
-
var ch:String;
|
118
|
-
// store the length in a local variable to reduce lookups
|
119
|
-
var len:Number = str.length;
|
120
|
-
|
121
|
-
// loop over all of the characters in the string
|
122
|
-
for ( var i:int = 0; i < len; i++ ) {
|
123
|
-
|
124
|
-
// examine the character to determine if we have to escape it
|
125
|
-
ch = str.charAt( i );
|
126
|
-
switch ( ch ) {
|
127
|
-
|
128
|
-
case '"': // quotation mark
|
129
|
-
s += "\\\"";
|
130
|
-
break;
|
131
|
-
|
132
|
-
//case '/': // solidus
|
133
|
-
// s += "\\/";
|
134
|
-
// break;
|
135
|
-
|
136
|
-
case '\\': // reverse solidus
|
137
|
-
s += "\\\\";
|
138
|
-
break;
|
139
|
-
|
140
|
-
case '\b': // bell
|
141
|
-
s += "\\b";
|
142
|
-
break;
|
143
|
-
|
144
|
-
case '\f': // form feed
|
145
|
-
s += "\\f";
|
146
|
-
break;
|
147
|
-
|
148
|
-
case '\n': // newline
|
149
|
-
s += "\\n";
|
150
|
-
break;
|
151
|
-
|
152
|
-
case '\r': // carriage return
|
153
|
-
s += "\\r";
|
154
|
-
break;
|
155
|
-
|
156
|
-
case '\t': // horizontal tab
|
157
|
-
s += "\\t";
|
158
|
-
break;
|
159
|
-
|
160
|
-
default: // everything else
|
161
|
-
|
162
|
-
// check for a control character and escape as unicode
|
163
|
-
if ( ch < ' ' ) {
|
164
|
-
// get the hex digit(s) of the character (either 1 or 2 digits)
|
165
|
-
var hexCode:String = ch.charCodeAt( 0 ).toString( 16 );
|
166
|
-
|
167
|
-
// ensure that there are 4 digits by adjusting
|
168
|
-
// the # of zeros accordingly.
|
169
|
-
var zeroPad:String = hexCode.length == 2 ? "00" : "000";
|
170
|
-
|
171
|
-
// create the unicode escape sequence with 4 hex digits
|
172
|
-
s += "\\u" + zeroPad + hexCode;
|
173
|
-
} else {
|
174
|
-
|
175
|
-
// no need to do any special encoding, just pass-through
|
176
|
-
s += ch;
|
177
|
-
|
178
|
-
}
|
179
|
-
} // end switch
|
180
|
-
|
181
|
-
} // end for loop
|
182
|
-
|
183
|
-
return "\"" + s + "\"";
|
184
|
-
}
|
185
|
-
|
186
|
-
/**
|
187
|
-
* Converts an array to it's JSON string equivalent
|
188
|
-
*
|
189
|
-
* @param a The array to convert
|
190
|
-
* @return The JSON string representation of <code>a</code>
|
191
|
-
*/
|
192
|
-
private function arrayToString( a:Array ):String {
|
193
|
-
// create a string to store the array's jsonstring value
|
194
|
-
var s:String = "";
|
195
|
-
|
196
|
-
// loop over the elements in the array and add their converted
|
197
|
-
// values to the string
|
198
|
-
for ( var i:int = 0; i < a.length; i++ ) {
|
199
|
-
// when the length is 0 we're adding the first element so
|
200
|
-
// no comma is necessary
|
201
|
-
if ( s.length > 0 ) {
|
202
|
-
// we've already added an element, so add the comma separator
|
203
|
-
s += ","
|
204
|
-
}
|
205
|
-
|
206
|
-
// convert the value to a string
|
207
|
-
s += convertToString( a[i] );
|
208
|
-
}
|
209
|
-
|
210
|
-
// KNOWN ISSUE: In ActionScript, Arrays can also be associative
|
211
|
-
// objects and you can put anything in them, ie:
|
212
|
-
// myArray["foo"] = "bar";
|
213
|
-
//
|
214
|
-
// These properties aren't picked up in the for loop above because
|
215
|
-
// the properties don't correspond to indexes. However, we're
|
216
|
-
// sort of out luck because the JSON specification doesn't allow
|
217
|
-
// these types of array properties.
|
218
|
-
//
|
219
|
-
// So, if the array was also used as an associative object, there
|
220
|
-
// may be some values in the array that don't get properly encoded.
|
221
|
-
//
|
222
|
-
// A possible solution is to instead encode the Array as an Object
|
223
|
-
// but then it won't get decoded correctly (and won't be an
|
224
|
-
// Array instance)
|
225
|
-
|
226
|
-
// close the array and return it's string value
|
227
|
-
return "[" + s + "]";
|
228
|
-
}
|
229
|
-
|
230
|
-
/**
|
231
|
-
* Converts an object to it's JSON string equivalent
|
232
|
-
*
|
233
|
-
* @param o The object to convert
|
234
|
-
* @return The JSON string representation of <code>o</code>
|
235
|
-
*/
|
236
|
-
private function objectToString( o:Object ):String
|
237
|
-
{
|
238
|
-
// create a string to store the object's jsonstring value
|
239
|
-
var s:String = "";
|
240
|
-
|
241
|
-
// determine if o is a class instance or a plain object
|
242
|
-
var classInfo:XML = describeType( o );
|
243
|
-
if ( classInfo.@name.toString() == "Object" )
|
244
|
-
{
|
245
|
-
// the value of o[key] in the loop below - store this
|
246
|
-
// as a variable so we don't have to keep looking up o[key]
|
247
|
-
// when testing for valid values to convert
|
248
|
-
var value:Object;
|
249
|
-
|
250
|
-
// loop over the keys in the object and add their converted
|
251
|
-
// values to the string
|
252
|
-
for ( var key:String in o )
|
253
|
-
{
|
254
|
-
// assign value to a variable for quick lookup
|
255
|
-
value = o[key];
|
256
|
-
|
257
|
-
// don't add function's to the JSON string
|
258
|
-
if ( value is Function )
|
259
|
-
{
|
260
|
-
// skip this key and try another
|
261
|
-
continue;
|
262
|
-
}
|
263
|
-
|
264
|
-
// when the length is 0 we're adding the first item so
|
265
|
-
// no comma is necessary
|
266
|
-
if ( s.length > 0 ) {
|
267
|
-
// we've already added an item, so add the comma separator
|
268
|
-
s += ","
|
269
|
-
}
|
270
|
-
|
271
|
-
s += escapeString( key ) + ":" + convertToString( value );
|
272
|
-
}
|
273
|
-
}
|
274
|
-
else // o is a class instance
|
275
|
-
{
|
276
|
-
// Loop over all of the variables and accessors in the class and
|
277
|
-
// serialize them along with their values.
|
278
|
-
for each ( var v:XML in classInfo..*.(
|
279
|
-
name() == "variable"
|
280
|
-
||
|
281
|
-
(
|
282
|
-
name() == "accessor"
|
283
|
-
// Issue #116 - Make sure accessors are readable
|
284
|
-
&& attribute( "access" ).charAt( 0 ) == "r" )
|
285
|
-
) )
|
286
|
-
{
|
287
|
-
// Issue #110 - If [Transient] metadata exists, then we should skip
|
288
|
-
if ( v.metadata && v.metadata.( @name == "Transient" ).length() > 0 )
|
289
|
-
{
|
290
|
-
continue;
|
291
|
-
}
|
292
|
-
|
293
|
-
// When the length is 0 we're adding the first item so
|
294
|
-
// no comma is necessary
|
295
|
-
if ( s.length > 0 ) {
|
296
|
-
// We've already added an item, so add the comma separator
|
297
|
-
s += ","
|
298
|
-
}
|
299
|
-
|
300
|
-
s += escapeString( v.@name.toString() ) + ":"
|
301
|
-
+ convertToString( o[ v.@name ] );
|
302
|
-
}
|
303
|
-
|
304
|
-
}
|
305
|
-
|
306
|
-
return "{" + s + "}";
|
307
|
-
}
|
308
|
-
|
309
|
-
|
310
|
-
}
|
311
|
-
|
312
|
-
}
|