Haxe Serialization Format

You can find a complete implementation of the serializer in haxe/std/haxe/Serializer.hx and for the unserializer in haxe/std/haxe/Unserializer.hx

Each value is prefixed with a single char, followed by some extra data depending on the value :

  • null : n
  • Int : z for zero, or i followed by the integer itself (ex : i465)
  • Float :
    • k for NaN
    • m for -Inf and p for +Inf
    • d followed by the float display (ex : d1.45e-8)
  • Bool :
    • t for true
    • f for false
  • String : y followed by the url encoded string length, then : and the url encoded string
    example : y10:hi%20there for "hi there".
    each String is then stored into a cache with a string number (starting from 0).
    When we encounter a String that has already been serialized, we can store instead R456 where 456 is the index of this string in the cache. The string cache is always enabled, it is distinct from the object cache.
  • Structure : o followed by a list of fields and ending with g
    The list of fields consists in pairs of key (field name) / value, the key being stored as a String.
    example : oy1:xi2y1:kng for { x : 2, k : null }
  • List : l followed by the list of serialized items, and ending with a h (ex : lnnh for a List containing two nulls)
  • Array : a followed by serialized items, and ending with a h
    if there are several consecutive nulls, we can store u5 instead of nnnnn
    example : ai1i2u4i7ni9h for [1,2,null,null,null,null,7,null,9]
  • Date : v followed by the date itself (ex : d2010-01-01 12:45:10)
  • Hash : b followed by the keys / values pairs and ending by h
    example : by1:xi2y1:knh for { "x" => 2, "k" => null }
  • IntHash : q followed by the keys/values pairs and ending by h. Each key is stored as :<int>.
    example : q:4n:5i45:6i7h for { 4 => null, 5 => 45, 6 => 7 }
  • haxe.io.Bytes : s followed by the length of base64 encoded bytes, then : and the bytes encoded in base64 using the codes A-Za-z0-9%:
    example : s3:AAA for 2 bytes equal to 0
    other example : s10:SGVsbG8gIQ for haxe.io.Bytes.ofString("Hello !")
  • Exception : x followed by the exception value
  • Class instance : c followed by the class name (serialized as String) followed by a list of fields (see Structure) and ending by g
    example : cy5:Pointy1:xzy1:yzg for new Point(0,0) (having two integer fields x and y)
  • Enum (by name) : w followed by the enum name, the constructor name (as String), the number of arguments and the arguments itself
    example : wy3:Fooy1:A0 for Foo.A (with no arguments)
    example : wy3:Fooy1:B2i4n for Foo.B(4,null)
  • Enum (by index) : same as by name but using j instead of w for prefix, and <int>: instead of the constructor name :
    example : wy3:Foo0:0 for Foo.A (with no arguments)
    example : wy3:Foo1:2i4n for Foo.B(4,null)
  • Custom : C followed by the class name, followed by custom serialized data, and ending with g
    example : Cy18:MyCustomSerializerzzg for example in serialization documentation.
  • Cached references : when object cache is enable, each object, class instance and enum instance (except String) is stored into a cache before serializing its content, and is assigned an unique index starting from 0. When an object which is already in the cache is serialized, we instead store r<int> where <int> is the index into the cache.

version #15752, modified 2012-12-02 10:14:05 by ppelleti