I have object that is returned from the server-side JavaScript, that is sent as a String, and I attempt to restore like:
/* function that determines whether or not a String should be a function (namely, if it is the string form of a function)
* Parameters:
* • str : the string to check
* Returns:
* • true if str should be a function, or false otherwise
* NOTE: the primary use for this function is for restoring stringified member functions of objects returned from the server
*/
function shouldBeFunction(str)
{
str = str.toString().trim();
// str should *not* be function iff it doesn't start with 'function'
if (str.indexOf('function') !== 0) return false;
// str should *not* be function iff it doesn't have a '(' and a ')'
if ((str.indexOf('(') === -1) || (str.indexOf(')') === -1)) return false;
// str should *not* be function iff it doesn't have a '{' and a '}'
if ((str.indexOf('{') === -1) || (str.indexOf('}') === -1)) return false;
return true;
}
/* reviver function for stringified objects that contain stringified methods
* Parameters :
* • key : the key of the object
* • value : the value of object[key]
*/
function objectReviver(key, value)
{
var DEBUG = false;
if ((typeof(value) === 'string') && (shouldBeFunction(value))) {
if (DEBUG) {
console.log('function string detected on property named : ' + key);
console.log('function text: " ' + value + '"');
}
// get arguments list, if there is one to get
var argsList = value.substring(value.indexOf('(') + 1, value.indexOf(')')).trim();
if (DEBUG) console.log('argsList == ' + argsList);
var functionBody = value.substring(value.indexOf('{') + 1, value.lastIndexOf('}')).trim();
if (DEBUG) console.log('functionBody == ' + functionBody);
if (argsList)
return new Function(argsList, functionBody);
return new Function(functionBody);
}
return value;
}
var myObj = JSON.parse(objStringFromServer, objectReviver);
However, when I inspect all of its methods, they appear to be anonymous functions that cause problems later on in my code! (Namely, I have a Map<Object, String>
that seems to deep-compare the Object
specified in get()
with any keys in the map, including its toString()
method.) How do I fix this?