Skip to content

Instantly share code, notes, and snippets.

@frostney
Created January 3, 2013 21:13
Show Gist options
  • Save frostney/4447330 to your computer and use it in GitHub Desktop.
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.
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