HaxeDoc2

(やまだぃちぅ) #1
Serialization configuration Serialization can be configured in two ways. For both a static vari-
able can be set to influence allhaxe.Serializerinstances, and a member variable can be set
to only influence a specific instance:

USE_CACHE,useCache:If true, repeated objects are serialized by reference. This can avoid
infinite loops for recursive data at the expense of longer serialization time. By default, this
value isfalse.

USE_ENUM_INDEX,useEnumIndex:If true, enum constructors are serialized by their index in-
stead of their name. This can make the serialization string shorter but breaks if enum con-
structors are inserted into the type before deserialization. By default, this value isfalse.

Deserialization behavior If the serialization result is stored and later used for deserialization,
care has to be taken to maintain compatibility when working with class and enum instances. It
is then important to understand exactly how unserialization is implemented.


  • The type has to be available in the runtime where the deserialization is made. If dead
    code elimination (8.2) is active, a type which is used only through serialization might be
    removed.

  • EachUnserializerhas a member variableresolverwhich is used to resolve classes
    and enums by name. Upon creation of theUnserializerthis is set toUnserializer.DEFAULT_-
    RESOLVER. Both that and the instance member can be set to a custom resolver.

  • Classes are resolved by name usingresolver.resolveClass(name). The instance is
    then created usingType.createEmptyInstance, which means that the class constructor
    is not called. Finally, the instance fields are set according to the serialized value.

  • Enums are resolved by name usingresolver.resolveEnum(name). The enum instance
    is then created usingType.createEnum, using the serialized argument values if available.
    If the constructor arguments were changed since serialization, the result is unspecified.


Custom (de)serialization If a class defines the member methodhxSerialize, that method is
called by the serializer and allows custom serialization of the class. Likewise, if a class defines
the member methodhxUnserializeit is called by the deserializer:
1 import haxe.Serializer;
2 import haxe.Unserializer;
3
4 class Main {
5
6 var x:Int;
7 var y:Int;
8
9 static function main() {
10 var s = Serializer.run(newMain(1, 2));
11 var c:Main = Unserializer.run(s);
12 trace(c.x); // 1
13 trace(c.y); // -1
14 }
15
16 function new(x, y){
17 this.x = x;

Free download pdf