js.evalJson

From Web Computing Documentation
Jump to: navigation, search

Description

Safely evaluates a JSON string. If it's successful it returns the corresponding object, if the string fails the evaluation it throws an exception.

js.evalJson( string )


string A JSON string representing a JavaScript object.


The function offers the same functionality as eval but it removes the variables and functions of the framework from the evaluation's namespace, so that strings containing malicious code cannot harm the framework itself. This function should always be used instead of JavaScripts eval.

If your application needs to work with JSON input files you can apply this function on the input strings received from the framework. In case the string fails to evaluate to a JavaScript object, the function will throw an exception just like eval. Because of this the function should be used inside a try-catch statement.

Examples

var JSONinput = js.input.read("myinput");
 
try
{
    var input = js.evalJson(JSONinput);
}
catch(e)
{
    return -1; //if JSONinput is not valid
}

After this code you can use input like an standard JavaScript object.

Personal tools
Modules