replace - Replacing data fields with code in JSON.stringify? -
so can replace property number, string, array, or object in json.stringify, so:
var myobj = { 'allstar': afunction; } function myreplacer(key, value) { if(key === 'allstar') { return 'newfunction()'; } } json.stringify(myobj, myreplacer); //returns '{"allstar": "newfunction()"}'
but can change instead returns '{"allstar": newfunction()}'
(without quotes around newfunction
)?
i assume typeof afunction == "function"
? if so, json.stringify(myobj)
not want do, return '{}'
i.e. object without properties, because functions not supported in json.
your desired result not valid json. newfunction()
without quotes not supported value (string, number, array, object, boolean, null).
edit: try return newfunction.tostring()
in replacer, should deliver function's source string. when converting json back, must eval()
string actual function.
Comments
Post a Comment