Git Product home page Git Product logo

podstream's Introduction

PODStream

PODStream will serialize and deserialize your fields from/to BytesInput / BytesOutput.

Given a position class such as:

@:build(podstream.SerializerMacro.build())
class Position
{
    @Short public var x:Float;
    @Short public var y:Float;

    public function new(x:Float, y:Float)
    {
        this.x = x;
        this.y = y;
    }
}

You can prefix your fields with:

  • @Short
  • @Int
  • @Float
  • @Bool
  • @Byte
  • @String

Note: Arrays are not supported.

In the example above the haxe macro will generate two methods which will transform your class to:

class Position
{
    public var _id:Int = 0;
    public static var __id:Int = 0;
    public var x:Float;
    public var y:Float;

    public function new(x:Float, y:Float)
    {
        this.x = x;
        this.y = y;
    }
    
    public function unserialize(bi)
    {
        x = bi.readInt16();
        y = bi.readInt16();
    }
    
    public function serialize(bo)
    {
        bo.writeInt16(Std.int(x));
        bo.writeInt16(Std.int(y));
    }
}

The library is used in two steps:

  • Mark your fields with the appropriate serialization
  • Pass your BytesInput and BytesOutput around

Redirections

You can redirect deserializations to other variables such as:

@Short('netx') var x:Float;

In this case the variable x will be serialized as a Short but will be unserialized and assigned to the variable netx. It can be helpful when you are sharing the same class between a client & a server.

Example

@:build(podstream.SerializerMacro.build())
class Position2
{
    @Short("netx") public var x:Float;
    @Short("nety") public var y:Float;
    public var netx:Float;
    public var nety:Float;

    public function new(x:Float, y:Float)
    {
        this.x = x;
        this.y = y;
    }
}

will generate:

class Position2
{
    public var _id:Int = 1;
    public static var __id:Int = 1;
    public var x:Float;
    public var y:Float;
    public var netx:Float;
    public var nety:Float;

    public function new(x:Float, y:Float)
    {
        this.x = x;
        this.y = y;
    }
    
    public function unserialize(bi)
    {
        netx = bi.readInt16();
        nety = bi.readInt16();
    }
    
    public function serialize(bo)
    {
        bo.writeInt16(Std.int(x));
        bo.writeInt16(Std.int(y));
    }
}

ID generation

Each class will be assigned a unique public & static ID such as myInstance._id and MyClass.__id.

Serialization datas

You can call podstream.SerializerMacro.getSerialized() from your application and get an Array<String> of the types serialized.

You can then use Type.resolveClass("YourClassName") to resolve the type from the string returned if needed.

podstream's People

Contributors

dvergar avatar

Watchers

paling avatar James Cloos 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.