Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 50 additions & 52 deletions jsol.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,66 +28,64 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
;(function(self) {
/**
JSOL stands for JavaScript Object Literal which is a string representing
an object in JavaScript syntax.

For example:
(function(exports){

{foo:"bar"} is equivalent to {"foo":"bar"} in JavaScript. Both are valid JSOL.
/**
JSOL stands for JavaScript Object Literal which is a string representing
an object in JavaScript syntax.

Note that {"foo":"bar"} is proper JSON[1] therefore you can use one of the many
JSON parsers out there like json2.js[2] or even the native browser's JSON parser,
if available.
For example:

However, {foo:"bar"} is NOT proper JSON but valid Javascript syntax for
representing an object with one key, "foo" and its value, "bar".
Using a JSON parser is not an option since this is NOT proper JSON.
{foo:"bar"} is equivalent to {"foo":"bar"} in JavaScript. Both are valid JSOL.

You can use JSOL.parse to safely parse any string that reprsents a JavaScript Object Literal.
JSOL.parse will throw an Invalid JSOL exception on function calls, function declarations and variable references.
Note that {"foo":"bar"} is proper JSON[1] therefore you can use one of the many
JSON parsers out there like json2.js[2] or even the native browser's JSON parser,
if available.

Examples:
However, {foo:"bar"} is NOT proper JSON but valid Javascript syntax for
representing an object with one key, "foo" and its value, "bar".
Using a JSON parser is not an option since this is NOT proper JSON.

JSOL.parse('{foo:"bar"}'); // valid
You can use JSOL.parse to safely parse any string that reprsents a JavaScript Object Literal.
JSOL.parse will throw an Invalid JSOL exception on function calls, function declarations and variable references.

JSOL.parse('{evil:(function(){alert("I\'m evil");})()}'); // invalid function calls
Examples:

JSOL.parse('{fn:function() { }}'); // invalid function declarations
JSOL.parse('{foo:"bar"}'); // valid

var bar = "bar";
JSOL.parse('{foo:bar}'); // invalid variable references
JSOL.parse('{evil:(function(){alert("I\'m evil");})()}'); // invalid function calls

[1] http://www.json.org
[2] http://www.json.org/json2.js
*/
if (!self.JSOL) {
self.JSOL = {};
}
// Used for trimming whitespace
var trim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g;
if (typeof self.JSOL.parse !== "function") {
self.JSOL.parse = function(text) {
// make sure text is a "string"
if (typeof text !== "string" || !text) {
return null;
}
// Make sure leading/trailing whitespace is removed
text = text.replace(trim, "");
// Make sure the incoming text is actual JSOL (or Javascript Object Literal)
// Logic borrowed from http://json.org/json2.js
if ( /^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
.replace(/(?:^|:|,)(?:\s*\[)+/g, ":")
/** everything up to this point is json2.js **/
/** this is the 5th stage where it accepts unquoted keys **/
.replace(/\w*\s*\:/g, ":")) ) {
return (new Function("return " + text))();
}
else {
throw("Invalid JSOL: " + text);
}
};
}
})(window);
JSOL.parse('{fn:function() { }}'); // invalid function declarations

var bar = "bar";
JSOL.parse('{foo:bar}'); // invalid variable references

[1] http://www.json.org
[2] http://www.json.org/json2.js
*/

// Used for trimming whitespace
var trim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g;
exports.parse = function(text) {
// make sure text is a "string"
if (!text || typeof text !== "string") {
return null;
}
// Make sure leading/trailing whitespace is removed
text = text.replace(trim, "");
// Make sure the incoming text is actual JSOL (or Javascript Object Literal)
// Logic borrowed from http://json.org/json2.js
if ( /^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
.replace(/(?:^|:|,)(?:\s*\[)+/g, ":")
/** everything up to this point is json2.js **/
/** this is the 5th stage where it accepts unquoted keys **/
.replace(/\w*\s*\:/g, ":")) ) {
return (new Function("return " + text))();
} else {
throw("Invalid JSOL: " + text);
}
};

})(exports ? exports : this['JSOL']={});