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: https://haxe.org/manual/std-serialization-format.html

Static variables

@:value(false)staticUSE_CACHE:Bool = false

Enables object caching during serialization to handle circular references and repeated objects.

Set USE_CACHE to true if the values you are serializing may contain circular references or repeated objects. This prevents infinite loops and ensures that shared references are preserved in the serialized output.

Enabling this option may also reduce the size of the resulting serialized string, but can have a minor performance impact.

This is a global default. You can override it per instance using the useCache field on a Serializer.

@:value(false)staticUSE_ENUM_INDEX:Bool = false

Serializes enum values using constructor indices instead of names.

When USE_ENUM_INDEX is set to true, enum constructors are serialized by their numeric index. This can reduce the size of the serialized data, especially for enums with long or frequently used constructor names.

However, using indices makes serialized data more fragile for long-term storage. If enum definitions change (e.g., by adding or removing constructors), the indices may no longer match the intended constructors.

This is a global default. You can override it per instance using the useEnumIndex field on a Serializer.

Static methods

staticrun(v:Dynamic):String

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().

Constructor

new()

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.

Variables

useCache:Bool

Determines whether this Serializer instance uses object caching.

When enabled, repeated references to the same object are serialized using references instead of duplicating data, reducing output size and preserving object identity.

See USE_CACHE for a complete description.

useEnumIndex:Bool

Determines whether this Serializer instance serializes enum values using their index instead of their constructor name.

Using indexes can reduce the size of the serialized data but may be less readable and more fragile if enum definitions change.

See USE_ENUM_INDEX for a complete description.

Methods

reset():Void

Resets the internal state of the Serializer, allowing it to be reused.

This does not affect the useCache or useEnumIndex properties; their values will remain unchanged after calling this method.

@:has_untypedserialize(v:Dynamic):Void

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.

toString():String

Return the String representation of this Serializer.

The exact format specification can be found here: https://haxe.org/manual/serialization/format