Serializer 0.1.1

The object of the defined type is restored from the character string and the character string to the object with Type.



Details

Author
Noritaka Horio
Current version
0.1.1
GitHub
holyshared/Serializer
Downloads
1688
Category
Utilities
Tags
Report
GitHub Issues

Releases


Dependencies

  • _self_/_current_:
    • Core/Type
    • Core/String
    • Core/Array
    • Core/Object

How to use

Definition of data type

A new data type is defined by using Type. The User type is defined in the following example.

function User(name, email){
    this.name = name;
    this.email = email;
}

User.prototype = {
    getName: function(){
        return this.name;
    },

    getEmail: function(){
        return this.email;
    }
};

new Type('User', User);

The type is registered in a Serializer.

The data type and the conversion rule are specified for the argument. The restoration from the character string uses and restores the regular expression.

//Register user
Serializer.register(User, {

    paturn: '{name}={email}', //It is {name} and {email} placeholder.

    params: {
        name: '\\w+',
        email: '\\w+'
    },

    serialize: function(object){
        return this.assemble({
            name: object.name,
            email: object.email
        });
    },

    deserialize: function(params){
        return new User(params.name, params.user);
    }

});

It actually uses it.

It converts into the character string with serialize, and it converts it from the character string into the object with deserialize.
It is possible to convert it from the character string into the object from the object to the character string by this freely.

//It converts it from the object to the character string. 
var user = new User('username', 'user-email');

var serializer = new Serializer();
var source = serializer.serialize(user); //Return username=user-email

//It restores it from the character string to the object.
var user = serializer.deserialize(source); //Return user object
console.log(user.getName()); //Character string username is output. 
console.log(user.getEmail()); //Character string user-email is output. 

Discuss

A note on comments here: These comments are moderated. No comments will show up until they are approved. Comments that are not productive (i.e. inflammatory, rude, etc) will not be approved.

Found a bug in this plugin? Please report it this repository's Github Issues.

blog comments powered by Disqus