Serializer.hx 15 KB

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