Question

This is a vary simple question, but can't find any docs on it.

I have a simple class:

public class User
{
    public var name:String;
    public var age:int;
}

I would like to serialize it using this:

var user:User = new User();
user.age = 15;
user.name = "mike";
//now serialize
var bytes:ByteArray = MessagePack.encoder.write(vo);

But I get an error:

Error: MessagePack handler for type base not found

How do I let MessagePack know what the User class is, how to serialize it?

Was it helpful?

Solution

MessagePack doesn't look like being able to serialize Class, like most serializer.

I suggest you to add a toObject method to your User class :

public function toObject():Object
{
    return {age:this.age, name:this.name}:
}

Then you can serialize your user :

var bytes:ByteArray = MessagePack.encoder.write(user.toObject());

You can also add a static fromObject method which takes an object and returns a new User initialized with this object.

static public function fromObject(o:Object):User
{
    var u = new User();
    u.age = o.age;
    u.name = o.name;

    return u;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top