Created
December 31, 2009 14:47
-
-
Save jfd/266753 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The Serializable module adds a set of handy utilities to make it easier to | |
* work with JSON-serialized classes. | |
* | |
* Usage: | |
* | |
* function MyClass(serialized_data) { | |
* this.init({ | |
* name: 'default name' | |
* }, serialized_data); | |
* }; | |
* | |
* MyClass.prototype.print_name = function() { | |
* puts("The name of the instance(" + this.type + ") is: " + this.name); | |
* } | |
* | |
* Serializable.make(MyClass, 'MyClass'); | |
* | |
* var instance = new MyClass(); | |
* var data = instance.strinifigy(); | |
* var instance2 = Serializable.parse(data); | |
* instance2.print_name(); | |
*/ | |
var Serializable = { | |
// Cache for serializable classes. | |
_classes: {}, | |
/** | |
* Generates a TypeError exception. | |
*/ | |
TypeError: function(name) { | |
return function() { | |
throw "Error parsing object: Could not match type: " + name; | |
} | |
}, | |
/** | |
* Take's a class and make it serializable. Specifiy the type_name to make | |
* the Class instanceable from the Serializable.parse function. | |
*/ | |
make: function(Class, type_name) { | |
// Adds a serialize init method to the class's prototype base. The method | |
// takes a dict of default values and a dict of user-defined values. The | |
// user-defined values are prioritized. The init method MUST be called in | |
// the Class constructor. | |
Class.prototype.init = function(defaults, props) { | |
var v = props || {}; | |
this._defaults = defaults; | |
for (var name in defaults) { | |
this[name] = v[name] || defaults[name]; | |
} | |
this['type'] = type_name; | |
} | |
// Adds a serialize strinifigy method to the specified class prototype. The | |
// method converts the object into a JSON string. All instance properties | |
// in the _props arguments is serialized. Leave the argument blank to | |
// serialize all predefined (defaults) properties. | |
Class.prototype.stringify = function(_props) { | |
var props = _props || Serializable.get_names_from_dict(this._defaults); | |
var length = props.length, obj = {}; | |
while (length--) { | |
var prop = props[length]; | |
obj[prop] = this[prop]; | |
} | |
return JSON.stringify(obj); | |
} | |
if (type_name) Serializable._classes[type_name] = Class; | |
}, | |
/** | |
* Parse's a JSON string and then create's an Class instance based on the | |
* type property | |
*/ | |
parse: function(json) { | |
var props = JSON.parse(json); | |
var t = props.type; | |
var Class = Serializable._classes[t] || Serializable.TypeError(t); | |
return new Class(props); | |
}, | |
/** | |
* Takes a dict and returns all property names in an array. | |
*/ | |
get_names_from_dict: function(dict) { | |
var result = []; | |
for (var name in dict) { | |
result.push(name); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment