Skip to content

Commit

Permalink
Add JSON.parse Reviver method to enable passing of rich objects
Browse files Browse the repository at this point in the history
  • Loading branch information
thebarge committed Jan 23, 2015
1 parent b8be8ee commit 5c1aef8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
35 changes: 34 additions & 1 deletion dist/bellhop.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@

try
{
data = JSON.parse(data);
data = JSON.parse(data, Bellhop.reviver);
}
catch(err)
{
Expand Down Expand Up @@ -446,6 +446,39 @@
this._sendLater = null;
};

/**
* When restoring from JSON via `JSON.parse`, we may pass a reviver function.
* In our case, this will check if the object has a specially-named property (`__classname`).
* If it does, we will attempt to construct a new instance of that class, rather than using a
* plain old Object. Note that this recurses through the object.
* See <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse">JSON.parse()</a>
* @method reviver
* @static
* @param {String} key each key name
* @param {Object} value Object that we wish to restore
* @return {Object} The object that was parsed - either cast to a class, or not
*/
Bellhop.reviver = function(key, value)
{
if(value && typeof value.__classname == "string")
{
var _class = include(value.__classname, false);
if(_class)
{
var rtn = new _class();
//if we may call fromJSON, do so
if(rtn.fromJSON)
{
rtn.fromJSON(value);
//return the cast Object
return rtn;
}
}
}
//return the object we were passed in
return value;
};

// Assign to the global namespace
window.Bellhop = Bellhop;

Expand Down
2 changes: 1 addition & 1 deletion dist/bellhop.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 34 additions & 1 deletion src/Bellhop.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@

try
{
data = JSON.parse(data);
data = JSON.parse(data, Bellhop.reviver);
}
catch(err)
{
Expand Down Expand Up @@ -445,6 +445,39 @@
this._sendLater = null;
};

/**
* When restoring from JSON via `JSON.parse`, we may pass a reviver function.
* In our case, this will check if the object has a specially-named property (`__classname`).
* If it does, we will attempt to construct a new instance of that class, rather than using a
* plain old Object. Note that this recurses through the object.
* See <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse">JSON.parse()</a>
* @method reviver
* @static
* @param {String} key each key name
* @param {Object} value Object that we wish to restore
* @return {Object} The object that was parsed - either cast to a class, or not
*/
Bellhop.reviver = function(key, value)
{
if(value && typeof value.__classname == "string")
{
var _class = include(value.__classname, false);
if(_class)
{
var rtn = new _class();
//if we may call fromJSON, do so
if(rtn.fromJSON)
{
rtn.fromJSON(value);
//return the cast Object
return rtn;
}
}
}
//return the object we were passed in
return value;
};

// Assign to the global namespace
window.Bellhop = Bellhop;

Expand Down

0 comments on commit 5c1aef8

Please sign in to comment.