Skip to content

Instantly share code, notes, and snippets.

@colelawrence
Created June 4, 2014 19:28
Show Gist options
  • Save colelawrence/3c703f3990b2174bf291 to your computer and use it in GitHub Desktop.
Save colelawrence/3c703f3990b2174bf291 to your computer and use it in GitHub Desktop.
Eval JSON, JS, CSON
var coffee = require("coffee-script")
var vm = require("vm")
// Based on https://github.com/bevry/cson/blob/master/src/lib/cson.coffee
exports.parse = function (src, isJson) {
try {
result = JSON.parse(src)
} catch (err) {
if (isJson)
throw err
try {
result = vm.runInNewContext(src)
} catch (err) {
// throw error for tree
result = coffee.eval(src, { sandbox:true })
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment