Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSerializer.hx
More file actions
547 lines (493 loc) · 13.1 KB
/
Copy pathSerializer.hx
File metadata and controls
547 lines (493 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
* Copyright (C)2005-2013 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe;
/**
The Serializer class can be used to encode values and objects into a String,
from which the Unserializer class can recreate the original representation.
This class can be used in two ways:
- create a new Serializer() instance, call its serialize() method with
any argument and finally retrieve the String representation from
toString()
- call Serializer.run() to obtain the serialized representation of a
single argument
Serialization is guaranteed to work for all haxe-defined classes, but may
or may not work for instances of external/native classes.
The specification of the serialization format can be found here:
`http://haxe.org/manual/serialization/format`
**/
class Serializer {
/**
If the values you are serializing can contain circular references or
objects repetitions, you should set USE_CACHE to true to prevent
infinite loops.
This may also reduce the size of serialization Strings at the expense of
performance.
This value can be changed for individual instances of Serializer by
setting their useCache field.
**/
public static var USE_CACHE = false;
/**
Use constructor indexes for enums instead of names.
This may reduce the size of serialization Strings, but makes them less
suited for long-term storage: If constructors are removed or added from
the enum, the indices may no longer match.
This value can be changed for individual instances of Serializer by
setting their useEnumIndex field.
**/
public static var USE_ENUM_INDEX = false;
static var BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%:";
var buf : StringBuf;
var cache : Array<Dynamic>;
var shash : haxe.ds.StringMap<Int>;
var scount : Int;
/**
The individual cache setting for `this` Serializer instance.
See USE_CACHE for a complete description.
**/
public var useCache : Bool;
/**
The individual enum index setting for `this` Serializer instance.
See USE_ENUM_INDEX for a complete description.
**/
public var useEnumIndex : Bool;
/**
Creates a new Serializer instance.
Subsequent calls to `this.serialize` will append values to the
internal buffer of this String. Once complete, the contents can be
retrieved through a call to `this.toString`.
Each Serializer instance maintains its own cache if this.useCache` is
true.
**/
public function new() {
buf = new StringBuf();
cache = new Array();
useCache = USE_CACHE;
useEnumIndex = USE_ENUM_INDEX;
shash = new haxe.ds.StringMap();
scount = 0;
}
/**
Return the String representation of `this` Serializer.
The exact format specification can be found here:
http://haxe.org/manual/serialization/format
**/
public function toString() {
return buf.toString();
}
/* prefixes :
a : array
b : hash
c : class
d : Float
e : reserved (float exp)
f : false
g : object end
h : array/list/hash end
i : Int
j : enum (by index)
k : NaN
l : list
m : -Inf
n : null
o : object
p : +Inf
q : haxe.ds.IntMap
r : reference
s : bytes (base64)
t : true
u : array nulls
v : date
w : enum
x : exception
y : urlencoded string
z : zero
M : haxe.ds.ObjectMap
C : custom
*/
function serializeString( s : String ) {
var x = shash.get(s);
if( x != null ) {
buf.add("R");
buf.add(x);
return;
}
shash.set(s,scount++);
#if old_serialize
// no more support for -D old_serialize due to 'j' reuse
#if error #end
#end
buf.add("y");
s = StringTools.urlEncode(s);
buf.add(s.length);
buf.add(":");
buf.add(s);
}
function serializeRef(v) {
#if (js && !python)
var vt = untyped __js__("typeof")(v);
#end
for( i in 0...cache.length ) {
#if (js && !python)
var ci = cache[i];
if( untyped __js__("typeof")(ci) == vt && ci == v ) {
#else
if( cache[i] == v ) {
#end
buf.add("r");
buf.add(i);
return true;
}
}
cache.push(v);
return false;
}
#if flash9
// only the instance variables
function serializeClassFields(v,c) {
var xml : flash.xml.XML = untyped __global__["flash.utils.describeType"](c);
var vars = xml.factory[0].child("variable");
for( i in 0...vars.length() ) {
var f = vars[i].attribute("name").toString();
if( !v.hasOwnProperty(f) )
continue;
serializeString(f);
serialize(Reflect.field(v,f));
}
buf.add("g");
}
#end
function serializeFields(v) {
for( f in Reflect.fields(v) ) {
serializeString(f);
serialize(Reflect.field(v,f));
}
buf.add("g");
}
/**
Serializes `v`.
All haxe-defined values and objects with the exception of functions can
be serialized. Serialization of external/native objects is not
guaranteed to work.
The values of `this.useCache` and `this.useEnumIndex` may affect
serialization output.
**/
public function serialize( v : Dynamic ) {
switch( Type.typeof(v) ) {
case TNull:
buf.add("n");
case TInt:
if( v == 0 ) {
buf.add("z");
return;
}
buf.add("i");
buf.add(v);
case TFloat:
if( Math.isNaN(v) )
buf.add("k");
else if( !Math.isFinite(v) )
buf.add(if( v < 0 ) "m" else "p");
else {
buf.add("d");
buf.add(v);
}
case TBool:
buf.add(if( v ) "t" else "f");
case TClass(c):
if( #if neko untyped c.__is_String #else c == String #end ) {
serializeString(v);
return;
}
if( useCache && serializeRef(v) )
return;
switch( #if (neko || cs || python) Type.getClassName(c) #else c #end ) {
case #if (neko || cs || python) "Array" #else cast Array #end:
var ucount = 0;
buf.add("a");
#if (flash9 || python)
var v : Array<Dynamic> = v;
#end
var l = #if (neko || flash9 || php || cs || java || python) v.length #elseif cpp v.__length() #else v[untyped "length"] #end;
for( i in 0...l ) {
if( v[i] == null )
ucount++;
else {
if( ucount > 0 ) {
if( ucount == 1 )
buf.add("n");
else {
buf.add("u");
buf.add(ucount);
}
ucount = 0;
}
serialize(v[i]);
}
}
if( ucount > 0 ) {
if( ucount == 1 )
buf.add("n");
else {
buf.add("u");
buf.add(ucount);
}
}
buf.add("h");
case #if (neko || cs || python) "List" #else cast List #end:
buf.add("l");
var v : List<Dynamic> = v;
for( i in v )
serialize(i);
buf.add("h");
case #if (neko || cs || python) "Date" #else cast Date #end:
var d : Date = v;
buf.add("v");
buf.add(d.toString());
case #if (neko || cs || python) "haxe.ds.StringMap" #else cast haxe.ds.StringMap #end:
buf.add("b");
var v : haxe.ds.StringMap<Dynamic> = v;
for( k in v.keys() ) {
serializeString(k);
serialize(v.get(k));
}
buf.add("h");
case #if (neko || cs || python) "haxe.ds.IntMap" #else cast haxe.ds.IntMap #end:
buf.add("q");
var v : haxe.ds.IntMap<Dynamic> = v;
for( k in v.keys() ) {
buf.add(":");
buf.add(k);
serialize(v.get(k));
}
buf.add("h");
case #if (neko || cs || python) "haxe.ds.ObjectMap" #else cast haxe.ds.ObjectMap #end:
buf.add("M");
var v : haxe.ds.ObjectMap<Dynamic,Dynamic> = v;
for ( k in v.keys() ) {
#if (js || flash8 || neko)
var id = Reflect.field(k, "__id__");
Reflect.deleteField(k, "__id__");
serialize(k);
Reflect.setField(k, "__id__", id);
#else
serialize(k);
#end
serialize(v.get(k));
}
buf.add("h");
case #if (neko || cs || python) "haxe.io.Bytes" #else cast haxe.io.Bytes #end:
var v : haxe.io.Bytes = v;
#if neko
var chars = new String(base_encode(v.getData(),untyped BASE64.__s));
#else
var i = 0;
var max = v.length - 2;
var charsBuf = new StringBuf();
var b64 = BASE64;
while( i < max ) {
var b1 = v.get(i++);
var b2 = v.get(i++);
var b3 = v.get(i++);
charsBuf.add(b64.charAt(b1 >> 2));
charsBuf.add(b64.charAt(((b1 << 4) | (b2 >> 4)) & 63));
charsBuf.add(b64.charAt(((b2 << 2) | (b3 >> 6)) & 63));
charsBuf.add(b64.charAt(b3 & 63));
}
if( i == max ) {
var b1 = v.get(i++);
var b2 = v.get(i++);
charsBuf.add(b64.charAt(b1 >> 2));
charsBuf.add(b64.charAt(((b1 << 4) | (b2 >> 4)) & 63));
charsBuf.add(b64.charAt((b2 << 2) & 63));
} else if( i == max + 1 ) {
var b1 = v.get(i++);
charsBuf.add(b64.charAt(b1 >> 2));
charsBuf.add(b64.charAt((b1 << 4) & 63));
}
var chars = charsBuf.toString();
#end
buf.add("s");
buf.add(chars.length);
buf.add(":");
buf.add(chars);
default:
cache.pop();
if( #if flash9 try v.hxSerialize != null catch( e : Dynamic ) false #elseif (cs || java || python) Reflect.hasField(v, "hxSerialize") #else v.hxSerialize != null #end ) {
buf.add("C");
serializeString(Type.getClassName(c));
cache.push(v);
v.hxSerialize(this);
buf.add("g");
} else {
buf.add("c");
serializeString(Type.getClassName(c));
cache.push(v);
#if flash9
serializeClassFields(v,c);
#else
serializeFields(v);
#end
}
}
case TObject:
if( useCache && serializeRef(v) )
return;
buf.add("o");
serializeFields(v);
case TEnum(e):
if( useCache && serializeRef(v) )
return;
cache.pop();
buf.add(useEnumIndex?"j":"w");
serializeString(Type.getEnumName(e));
#if neko
if( useEnumIndex ) {
buf.add(":");
buf.add(v.index);
} else
serializeString(new String(v.tag));
buf.add(":");
if( v.args == null )
buf.add(0);
else {
var l : Int = untyped __dollar__asize(v.args);
buf.add(l);
for( i in 0...l )
serialize(v.args[i]);
}
#elseif flash9
if( useEnumIndex ) {
buf.add(":");
buf.add(v.index);
} else
serializeString(v.tag);
buf.add(":");
var pl : Array<Dynamic> = v.params;
if( pl == null )
buf.add(0);
else {
buf.add(pl.length);
for( p in pl )
serialize(p);
}
#elseif cpp
if( useEnumIndex ) {
buf.add(":");
buf.add(v.__Index());
} else
serializeString(v.__Tag());
buf.add(":");
var pl : Array<Dynamic> = v.__EnumParams();
if( pl == null )
buf.add(0);
else {
buf.add(pl.length);
for( p in pl )
serialize(p);
}
#elseif php
if( useEnumIndex ) {
buf.add(":");
buf.add(v.index);
} else
serializeString(v.tag);
buf.add(":");
var l : Int = untyped __call__("count", v.params);
if( l == 0 || v.params == null)
buf.add(0);
else {
buf.add(l);
for( i in 0...l )
serialize(untyped __field__(v, __php__("params"), i));
}
#elseif (java || cs || python)
if( useEnumIndex ) {
buf.add(":");
buf.add(Type.enumIndex(v));
} else
serializeString(Type.enumConstructor(v));
buf.add(":");
var arr:Array<Dynamic> = Type.enumParameters(v);
if (arr != null)
{
buf.add(arr.length);
for (v in arr)
serialize(v);
} else {
buf.add("0");
}
#else
if( useEnumIndex ) {
buf.add(":");
buf.add(v[1]);
} else
serializeString(v[0]);
buf.add(":");
var l = v[untyped "length"];
buf.add(l - 2);
for( i in 2...l )
serialize(v[i]);
#end
cache.push(v);
case TFunction:
throw "Cannot serialize function";
default:
#if neko
if( untyped (__i32__kind != null && __dollar__iskind(v,__i32__kind)) ) {
buf.add("i");
buf.add(v);
return;
}
#end
throw "Cannot serialize "+Std.string(v);
}
}
public function serializeException( e : Dynamic ) {
buf.add("x");
#if flash9
if( untyped __is__(e,__global__["Error"]) ) {
var e : flash.errors.Error = e;
var s = e.getStackTrace();
if( s == null )
serialize(e.message);
else
serialize(s);
return;
}
#end
serialize(e);
}
/**
Serializes `v` and returns the String representation.
This is a convenience function for creating a new instance of
Serializer, serialize `v` into it and obtain the result through a call
to toString().
**/
public static function run( v : Dynamic ) {
var s = new Serializer();
s.serialize(v);
return s.toString();
}
#if neko
static var base_encode = neko.Lib.load("std","base_encode",2);
#end
}
You can’t perform that action at this time.
