Guest User

Untitled

a guest
Apr 27th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. /**
  2. * The Serializable module adds a set of handy utilities to make it easier to
  3. * work with JSON-serialized classes.
  4. *
  5. * Usage:
  6. *
  7. * function MyClass(serialized_data) {
  8. * this.init({
  9. * name: 'default name'
  10. * }, serialized_data);
  11. * };
  12. *
  13. * MyClass.prototype.print_name = function() {
  14. * puts("The name of the instance(" + this.type + ") is: " + this.name);
  15. * }
  16. *
  17. * Serializable.make(MyClass, 'MyClass');
  18. *
  19. * var instance = new MyClass();
  20. * var data = instance.strinifigy();
  21. * var instance2 = Serializable.parse(data);
  22. * instance2.print_name();
  23. */
  24. var Serializable = {
  25.  
  26. // Cache for serializable classes.
  27. _classes: {},
  28.  
  29. /**
  30. * Generates a TypeError exception.
  31. */
  32. TypeError: function(name) {
  33. return function() {
  34. throw "Error parsing object: Could not match type: " + name;
  35. }
  36. },
  37.  
  38. /**
  39. * Take's a class and make it serializable. Specifiy the type_name to make
  40. * the Class instanceable from the Serializable.parse function.
  41. */
  42. make: function(Class, type_name) {
  43.  
  44. // Adds a serialize init method to the class's prototype base. The method
  45. // takes a dict of default values and a dict of user-defined values. The
  46. // user-defined values are prioritized. The init method MUST be called in
  47. // the Class constructor.
  48. Class.prototype.init = function(defaults, props) {
  49. var v = props || {};
  50. this._defaults = defaults;
  51. for (var name in defaults) {
  52. this[name] = v[name] || defaults[name];
  53. }
  54. this['type'] = type_name;
  55. }
  56.  
  57. // Adds a serialize strinifigy method to the specified class prototype. The
  58. // method converts the object into a JSON string. All instance properties
  59. // in the _props arguments is serialized. Leave the argument blank to
  60. // serialize all predefined (defaults) properties.
  61. Class.prototype.stringify = function(_props) {
  62. var props = _props || Serializable.get_names_from_dict(this._defaults);
  63. var length = props.length, obj = {};
  64. while (length--) {
  65. var prop = props[length];
  66. obj[prop] = this[prop];
  67. }
  68. return JSON.stringify(obj);
  69. }
  70.  
  71. if (type_name) Serializable._classes[type_name] = Class;
  72. },
  73.  
  74. /**
  75. * Parse's a JSON string and then create's an Class instance based on the
  76. * type property
  77. */
  78. parse: function(json) {
  79. var props = JSON.parse(json);
  80. var t = props.type;
  81. var Class = Serializable._classes[t] || Serializable.TypeError(t);
  82. return new Class(props);
  83. },
  84.  
  85. /**
  86. * Takes a dict and returns all property names in an array.
  87. */
  88. get_names_from_dict: function(dict) {
  89. var result = [];
  90. for (var name in dict) {
  91. result.push(name);
  92. }
  93. return result;
  94. }
  95. }
Add Comment
Please, Sign In to add comment