Abitbol {Serializable} logo Abitbol {Serializable}

Using Abitbol Serializable

Using Abitbol Serializable classes is easy: just create your class like any other Abitbol class and that's it. Abitbol Serializable classes can automatically serialize any computed property that have a getter and a setter:

var SerializableClass = require("abitbol-serializable");

var Demo = SerializableClass.$extend({

    __name__: "Demo",                     // Must be set to the class name

    __init__: function(params) {          // The constructor takes an object containing values for computed properties (optional)
        this.$data.myProp = "foo";        // Default values for computed properties
        this.$data.myProp2 = "bar";
        this.$data.myProp3 = "baz";
        this.$super(params);              // Call the super constructor to apply given values
        ...                               // Any other initialization code you need
    },

    getMyProp: function() {               // this.myProp: serialized propery (getter + setter)
        return this.$data.myProp;
    },

    setMyProp: function(value) {
        this.$data.myProp = value;
    },

    getMyProp2: function() {              // this.myProp2: not serialized (no setter)
        return this.$data.myProp2;
    },

    getMyProp3: function() {              // this.myProp3: not serialized (annotated with "@serializable false")
        "@serializable false";
        return this.$data.myProp3;
    },

    setMyProp3: function(value) {
        this.$data.myProp3 = value;
    }

});

SerializableClass.$register(Demo);        // Register the class (allows automatic unserialization)

Using the class is also simple:

var demo = new Demo({
    myProp: "foobar"
});

Serialization

To serialize, just call the serialize() method:

var data = demo.serialize();

// -> {
//        __name__: "Demo",
//        id: "<an autogenerated uuid>",
//        myProp: "foobar"
//    }

Unserialization

There is three way to unserialize an Abitbol Serializable class:

  • using the constructor,
  • using the unserialize() method,
  • and using the $unserialize() static method.

constructor

var demo = new Demo({
    ...  // your data (any serializable property)
});

NOTE: To allow this unserialization method to work, you have to call the $super() method of your constructor with the params:

var MyClass = SerializableClass.$extend({

    __init__: function(params) {
        ...
        this.$super(params);
        ...
    }

});

serializableClassInstance.unserialize()

var demo = new Demo();
demo.unserialize({
    ...  // your data
});

SerializableClass.$unserialize()

var demo = SerializableClass.$unserialize({
    __name__: "Demo",  // The class name
    ...  // your data
});

NOTE: To allow this unserialization method to work, you have to set an unique __name__ to the class and to register it:

var MyClass = SerializableClass.$extend({

    __name__: "MyClass",  // Unique classe name

});

// Register the class so Abitbol Serializable is able to
// unserialize it automatically
SerializableClass.$register(MyClass);

Custom Serializers

If your class contains serializable properties that are not built-in types (String, Number, Boolean, Object and Array), nor a registered SerializableClass you have to define custom serialization / unserialization functions:

SerializableClass.$addSerializer("serializerName", {

    class: YourClass,  // not required if you only use the `@serializer` annotation

    serialize: function(data) {
        // ...
        return serializedData;
    },

    unserialize: function(data) {
        // ...
        return unserializedData;
    }

});

And then you can annotates all properties that will require your custom serializer (if you defined a class in the serializer, this is not required):

var MyClass = SerializableClass.$extend({

    getFoobar: function() {
        "@serializer serializerName";
        // ...
    },

    setFoobar: function(value) {
        // ...
    }

});

Clonning a Serializable Class

To clone a class, just call the clone method of the class:

var demo = new Demo();
var demo2 = demo.clone();

NOTE: The id property if the cloned class is automatically regenerated to a new random uuid.

About the "id" property

Abitbol Serializable generate automatically an uuid for each class instances. You can use it safely to reference classes into other classes without making circular references.

Trouble shooting

If you have any problem using the Abitbol Serializable classes, first check the following points:

  • Check your class have its __name__ attribute defined to something unique,
  • Check you have registered your class with SerializableClass.$register(YourClass),
  • Check you have no circular references into your classes.

If you need further help, please open an issue on Github: