This is almost definitely a very, very bad idea. Instead of trying to 
serialise your code into JSON, you can just put it in a regular JavaScript 
file and require() it. For example, instead of this:

// code.json
{"myFunction":"function(a) { console.log(a); }"}

var code = JSON.parse(fs.readFileSync("./code.json"));
/* magic, insecure function stuff from your post */
code.myFunction("hello");

Do this:

// code.js
exports.myFunction = function(a) { console.log(a); }

var code = require("./code.js");
/* look ma, no magic */
code.myFunction("hello");

I shudder to think of why you might be doing things the way you are - are 
you trying to fetch code from somewhere else and run it?

On Friday, 25 July 2014 03:09:19 UTC+10, Rohit Harchandani wrote:
>
> Hi,
> This is the code that I am using to parse the JSON string.
>
> var recData = JSON.parse(recDataString, function (key, value) {
>
>     if (value && (typeof value === 'string') && value.indexOf("function") === 
> 0) {
>
>         var jsFunc = new Function('return ' + value)();
>
>         return jsFunc;
>
>     }
>
>     return value;
>
> });
>
>  console.log( "Type: " + typeof recData["runFunction"] )
>
>
> runFunction in the json is a function which is something like this:
>
>
> function() {
>
>     require("fs");
>
>
>
>
> On Thu, Jul 24, 2014 at 2:58 AM, Ryan Schmidt <[email protected] 
> <javascript:>> wrote:
>
>>
>> On Jul 23, 2014, at 6:46 PM, Rohit Harchandani wrote:
>>
>> > Hi, I have just started playing around with node.js for a project. I 
>> parsed a JSON object which had a javascript function and when I tried to 
>> execute the function on the node.js server, I kept getting this error:
>> > ReferenceError: require is not defined
>> > I am sure i parsed the JSON object correctly to get the function, but 
>> not sure how to run this function.
>>
>> The JSON format does not accommodate functions, only data.
>>
>> Perhaps you should show us some code so that we can more easily 
>> understand the problem you're encountering.
>>
>> --
>> Job board: http://jobs.nodejs.org/
>> New group rules: 
>> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
>> Old group rules: 
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> ---
>> You received this message because you are subscribed to the Google Groups 
>> "nodejs" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/nodejs/B5AA737B-3074-4B3C-9551-F1AB89DA6941%40ryandesign.com
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/2bad96a3-2419-443c-969e-37241d75aa05%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to