HaxeDoc2

(やまだぃちぅ) #1

10.7 Serialization


Many runtime values can be serialized and deserialized using thehaxe.Serializerandhaxe.Unserializer
classes. Both support two usages:

1.Create an instance and continuously call theserialize/unserializemethod to handle
multiple values.

2.Call their staticrunmethod to serialize/deserialize a single value.

The following example demonstrates the first usage:
1 import haxe.Serializer;
2 import haxe.Unserializer;
3
4 class Main {
5 static function main() {
6 var serializer = new Serializer();
7 serializer.serialize("foo");
8 serializer.serialize(12);
9 var s = serializer.toString();
10 trace(s); // y3:fooi12
11
12 var unserializer = new Unserializer(s);
13 trace(unserializer.unserialize()); // foo
14 trace(unserializer.unserialize()); // 12
15 }
16 }


The result of the serialization (here stored in local variables) is a String (10.1) and can
be passed around at will, even remotely. Its format is described inSerialization format(Sec-
tion 10.7.1).

Supported values


  • null

  • Bool,IntandFloat(including infinities andNaN)

  • String

  • Date

  • haxe.io.Bytes(encoded as base64)

  • Array(10.2.1) andList(10.2.3)

  • haxe.ds.StringMap,haxe.ds.IntMapandhaxe.ds.ObjectMap

  • anonymous structures (2.5)

  • Haxe class instances (2.3) (not native ones)

  • enum instances (2.4)

Free download pdf