Skip to content

Deep Copy / Deep Equal / Serialize / Unserialize Library for Haxe

License

Notifications You must be signed in to change notification settings

shohei909/TypePacker

Repository files navigation

TypePacker

TypePacker provides the following functionality by using type information collected at compile time macro.

  • Deep Clone
  • Structural Equality (Deep Equal)
  • Serialize / Unserialize

Deep Clone

var cloneData = typepacker.core.TypeUtil.clone("Array<Int>", [0, 1, 2]);

The typepacker.core.Clone interface automatically implements the clone function.

import typepacker.core.Clone;

class SampleData implements Clone
{
	public var a:Int;
	public var b:Float;
	
	public function new()
	{
		this.a = 1.0;
		this.b = 2.0;
	}
}

class Main
{
	public static function main():Void
	{
		var data = new SampleData();
		var cloneData = data.clone();
	}
}

Please refer to the test cases for more detailed usage.

Deep Equal

import typepacker.core.TypeUtil;

class SampleData
{
	public var a:Int;
	public var b:Float;
	
	public function new()
	{
		this.a = 1.0;
		this.b = 2.0;
	}
}

class Main
{
	public static function main():Void
	{
		var a = new SampleData();
		var b = new SampleData();
		var cloneData = TypeUtil.isSame("SampleData", a, b); // true
	}
}

Serialize / Unserialize

JSON

var jsonString = typepacker.json.Json.print("Array<Int>", [0, 1, 2]);
var arrayData = typepacker.json.Json.parse("Array<Int>", jsonString);

This serialization method is suited for data persistence.

Please refer to the test cases for more detailed usage.

TypePacker Binary Format

var bytearray = typepacker.bytes.BytesPack.print("Array<Int>", [0, 1, 2]);
var arrayData = typepacker.bytes.BytesPack.parse("Array<Int>", bytearray);

This serialization method is suitable for data communications. It has a smaller data size than Json, but compatibility between versions is more difficult.

Please refer to the test cases for more detailed usage.

Other formats

Serialization is also possible for any formats compatible with JSON. For example, Message Pack / Yaml.

Please refer to the Yaml test case.

Supported type

Deep Clone / Deep Equal / Serialize / Unserialize is available for the following types.

  • Primitive Type(Int / Float / Bool)
  • Collection(Array<T> / haxe.ds.List<T> / haxe.ds.Vector<T>)
  • String
  • Enum
  • Anonymous Type
  • Abstract
  • Typedef
  • haxe.io.Bytes
  • StringMap / IntMap
  • haxe.DynamicAccess<T>
  • Null<T>
  • Type (Class<Dynamic> / Enum<Dynamic>)

Unsupported

  • haxe.ds.EnumMap / haxe.ds.ObjectMap / haxe.ds.WeakMap
  • Function Type
  • etc...

Recursive types are supported, but instances with circular references are not supported.

Metadata for fields

  • @:serializeAlias("alias")
  • @:serializeToArray
  • @:noPack

About

Deep Copy / Deep Equal / Serialize / Unserialize Library for Haxe

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages