Giter Club home page Giter Club logo

haxeserializer's Introduction

HaxeSerializer

Macro helper for haxe.Serialize and haxe.Unserialize

Its goal is to help you to serialize and unserialize your custom class

class Test implements ISerializable
{
	@serialize public var serialized : Int = 0;
	public var notSerialized : Int = 0;
}

var t : Test = new Test();
t.serialized = 1;
t.notSerialized = 1;
var s = haxe.Serialize(t);
var tbis : Test = haxe.Unserialize(t);
trace(tbis.serialized); // 1
trace(tbis.notSerialized); // 0

If you try to serialize the following class with haxe.Serialize, only serialized will be outputed.

Here is a full usage sample from a personal project

package ;
import flash.Lib;
import flash.Memory;
import flash.system.Capabilities;
import flash.utils.ObjectOutput;
import flixel.util.FlxSave;
import haxe.Serializer;
import haxe.Unserializer;
import map.Level;
import utils.ISerializable;

/**
 * ...
 * @author Masadow
 */
class Save implements ISerializable
{
	private static var shared : FlxSave = null;
	
	@serialize public var money : Int;
	@serialize public var power : Int;
	@serialize public var powerPrice : Int;
	@serialize public var firerate : Float;
	@serialize public var fireratePrice : Int;
	@serialize public var target : Int;
	@serialize public var targetPrice : Int;
	@serialize public var level(default, null) : Array<Level>;

	public function new() 
	{
		init();
		flush(false);
	}
	
	private function init()
	{
		money = 0;
		power = 20;
		powerPrice = 200;
		firerate = 0.5;
		fireratePrice = 300;
		target = 1;
		targetPrice = 1000;

		//Reset the first lvl
		level = new Array<Level>();
		level.push(new Level(1));
	}
	
	public static function load(Create : Bool = true) : Save
	{
		if (shared == null)
		{
			shared = new FlxSave();
			shared.bind("save");
			//shared.erase();
			//shared.bind("save");
			if (Create && (!Std.is(shared.data.content, String) || cast(shared.data.content, String).length == 0))
			{
				new Save();
			}
		}
		trace(Unserializer.run(shared.data.content));
		return Unserializer.run(shared.data.content);
	}
	
	public function flush(Fast : Bool = true)
	{
		shared.data.content = Serializer.run(this);
		shared.flush();
		
		if (!Fast)
		{
			shared.close();
			shared.destroy();
			shared = null;
			load(false);
		}
	}
	
	public function reset()
	{
		shared.erase();
		shared.close();
		Save.load();
	}
	
}

haxeserializer's People

Contributors

masadow avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.