Created
January 3, 2013 21:13
-
-
Save frostney/4447330 to your computer and use it in GitHub Desktop.
Alternative approach to converting CSON (CoffeeScript Object Notation) to JSON (JavaScript Object Notation) which does not require outer curly brackets to be present in the CSON.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs = require 'fs' | |
path = require 'path' | |
coffeeScript = require 'coffee-script' | |
convertCSON = (csonObject) -> | |
return unless csonObject? | |
completeObject = | |
"csonObject = { | |
#{csonObject} | |
}" | |
compiledObject = coffeeScript.compile completeObject, {bare: true} | |
new Function("#{compiledObject}; return csonObject;")() | |
convertCSONFile = (filename, callback) -> | |
fs.readFile filename, (err, data) -> | |
throw err if err | |
callback convertCSON(data) | |
convertCSONFileSync = (filename) -> | |
fileData = fs.readFileSync filename, 'utf-8' | |
convertCSON fileData | |
exports = | |
convertCSON: convertCSON | |
convertCSONFile: convertCSONFile | |
convertCSONFileSync: convertCSONFileSync |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment