包详细信息

cson

bevry358.8kArtistic-2.08.4.0

CoffeeScript-Object-Notation Parser. Same as JSON but for CoffeeScript objects.

coffeescript, cson, esnext, javascript

自述文件

CSON

Status of the GitHub Workflow: bevry NPM version NPM downloads
GitHub Sponsors donate button ThanksDev donate button Patreon donate button Liberapay donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button
Discord server badge Twitch community badge

CoffeeScript-Object-Notation. Same as JSON but for CoffeeScript objects.

Projects using CSON.

Projects using CSON Parser directly.

Since v2, this CSON package is a higher-level wrapper around the lower-level CSON Parser.

What is CSON?

Everyone knows JSON, it's the thing that looks like this:

{
  "greatDocumentaries": [
    "earthlings.com",
    "forksoverknives.com",
    "cowspiracy.com"
  ],
  "importantFacts": {
    "emissions": "Livestock and their byproducts account for at least 32,000 million tons of carbon dioxide (CO2) per year, or 51% of all worldwide greenhouse gas emissions.\nGoodland, R Anhang, J. “Livestock and Climate Change: What if the key actors in climate change were pigs, chickens and cows?”\nWorldWatch, November/December 2009. Worldwatch Institute, Washington, DC, USA. Pp. 10–19.\nhttp://www.worldwatch.org/node/6294",
    "landuse": "Livestock covers 45% of the earth’s total land.\nThornton, Phillip, Mario Herrero, and Polly Ericksen. “Livestock and Climate Change.” Livestock Exchange, no. 3 (2011).\nhttps://cgspace.cgiar.org/bitstream/handle/10568/10601/IssueBrief3.pdf",
    "burger": "One hamburger requires 660 gallons of water to produce – the equivalent of 2 months’ worth of showers.\nCatanese, Christina. “Virtual Water, Real Impacts.” Greenversations: Official Blog of the U.S. EPA. 2012.\nhttp://blog.epa.gov/healthywaters/2012/03/virtual-water-real-impacts-world-water-day-2012/\n“50 Ways to Save Your River.” Friends of the River.\nhttp://www.friendsoftheriver.org/site/PageServer?pagename=50ways",
    "milk": "1,000 gallons of water are required to produce 1 gallon of milk.\n“Water trivia facts.” United States Environmental Protection Agency.\nhttp://water.epa.gov/learn/kids/drinkingwater/water_trivia_facts.cfm#_edn11",
    "more": "http://cowspiracy.com/facts"
  }
}

Now let's write the same thing in CSON:

# Comments!!!

# An Array with no commas!
greatDocumentaries: [
    'earthlings.com'
    'forksoverknives.com'
    'cowspiracy.com'
]

# An Object without braces!
importantFacts:
    # Multi-Line Strings! Without Quote Escaping!
    emissions: '''
        Livestock and their byproducts account for at least 32,000 million tons of carbon dioxide (CO2) per year, or 51% of all worldwide greenhouse gas emissions.
        Goodland, R Anhang, J. “Livestock and Climate Change: What if the key actors in climate change were pigs, chickens and cows?”
        WorldWatch, November/December 2009. Worldwatch Institute, Washington, DC, USA. Pp. 10–19.
        http://www.worldwatch.org/node/6294
        '''

    landuse: '''
        Livestock covers 45% of the earth’s total land.
        Thornton, Phillip, Mario Herrero, and Polly Ericksen. “Livestock and Climate Change.” Livestock Exchange, no. 3 (2011).
        https://cgspace.cgiar.org/bitstream/handle/10568/10601/IssueBrief3.pdf
        '''

    burger: '''
        One hamburger requires 660 gallons of water to produce – the equivalent of 2 months’ worth of showers.
        Catanese, Christina. “Virtual Water, Real Impacts.” Greenversations: Official Blog of the U.S. EPA. 2012.
        http://blog.epa.gov/healthywaters/2012/03/virtual-water-real-impacts-world-water-day-2012/
        “50 Ways to Save Your River.” Friends of the River.
        http://www.friendsoftheriver.org/site/PageServer?pagename=50ways
        '''

    milk: '''
        1,000 gallons of water are required to produce 1 gallon of milk.
        “Water trivia facts.” United States Environmental Protection Agency.
        http://water.epa.gov/learn/kids/drinkingwater/water_trivia_facts.cfm#_edn11
        '''

    more: 'http://cowspiracy.com/facts'

Which is far more lenient than JSON, way nicer to write and read, no need to quote and escape everything, has comments and readable multi-line strings, and won't fail if you forget a comma.

Using CSON

Via the Command Line

Use CSON with the command line with:

# Convert a JSON file into a CSON file
json2cson in.json > out.cson
# Same thing via piping
cat in.json | json2cson > out.cson

# Convert a CSON file into a JSON file
cson2json in.cson > out.json
# Same thing via piping
cat in.cson | cson2json > out.json

Requires a global CSON install: npm install -g cson

Via the API

Include CSON:

var CSON = require('cson')

Each method can be executed without a callback like so:

var result = CSON.createCSONString({a:{b:'c'}}, {/* optional options argument */})
if ( result instanceof Error ) {
    console.log(result.stack)
} else {
    console.log(result)
}

Or via a callback like so:

CSON.createCSONString({a:{b:'c'}}, {/* optional options argument */}, function(err,result){
    console.log(err, result)
})

Executing the method with a callback still executes the method synchronously.

Click the below function names to open more detailed documentation.

Create Strings

Parse Strings

Parse Files

Require Files

Install

npm

Install Globally

  • Install: npm install --global cson
  • Executables: cson, cson2json, json2cson

Install Locally

  • Install: npm install --save cson
  • Executables: npx cson, npx cson2json, npx json2cson
  • Import: import * as pkg from ('cson')
  • Require: const pkg = require('cson')

Editions

This package is published with the following editions:

  • cson/source/index.coffee is CoffeeScript source code with Require for modules
  • cson aliases cson/edition-esnext/index.js
  • cson/edition-esnext/index.js is CoffeeScript compiled for Node.js 6 || 8 || 10 || 12 || 14 || 16 || 18 || 20 || 21 with Require for modules

History

Discover the release history by heading on over to the HISTORY.md file.

Backers

Code

Discover how to contribute via the CONTRIBUTING.md file.

Authors

Maintainers

Contributors

Finances

GitHub Sponsors donate button ThanksDev donate button Patreon donate button Liberapay donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button

Sponsors

  • Andrew Nesbitt — Software engineer and researcher
  • Balsa — We're Balsa, and we're building tools for builders.
  • Codecov — Empower developers with tools to improve code quality and testing.
  • Poonacha Medappa
  • Rob Morris
  • Sentry — Real-time crash reporting for your web apps, mobile apps, and games.
  • Syntax — Syntax Podcast

Donors

License

Unless stated otherwise all works are:

and licensed under:

更新日志

History

v8.4.0 2023 December 29

v8.3.0 2023 December 27

v8.2.0 2023 November 24

v8.1.0 2023 November 21

v8.0.0 2023 November 18

  • Updated CLI to support autodetection of conversion, as well as supporting --cson2json and --json2cson args, also added cson bin
  • Updated dependencies, base files, and editions using boundation
  • Minimum required Node.js version changed from node: >=10 to node: >=6 adapting to ecosystem changes

v7.20.0 2020 September 4

v7.19.0 2020 August 18

v7.18.0 2020 August 4

v7.17.0 2020 July 22

v7.16.0 2020 July 22

v7.15.0 2020 July 3

v7.14.0 2020 July 3

v7.13.0 2020 July 3

v7.12.0 2020 June 25

v7.11.0 2020 June 21

v7.10.0 2020 June 21

v7.9.0 2020 June 20

v7.8.0 2020 June 20

v7.7.0 2020 June 10

v7.6.0 2020 June 10

v7.5.0 2020 May 22

v7.4.0 2020 May 21

v7.3.0 2020 May 12

v7.2.0 2020 May 11

v7.1.0 2020 May 4

v7.0.0 2020 March 26

  • Updated dependencies, base files, and editions using boundation
  • Minimum required node version changed from node: >=8 to node: >=10 to keep up with mandatory ecosystem changes

v6.9.0 2019 December 18

  • Fixed broken json2cson and cson2json executables (regression since v6.0.0)
  • Updated dependencies, base files, and editions using boundation

v6.8.0 2019 December 9

v6.7.0 2019 December 1

v6.6.0 2019 December 1

v6.5.0 2019 December 1

v6.4.0 2019 November 18

v6.3.0 2019 November 18

v6.2.0 2019 November 13

v6.1.0 2019 November 13

v6.0.0 2019 November 11

  • As node v4 is no longer LTS, CSON can now use CoffeeScript v2
  • Minimum supported node version changed from v0.8 to the lowest LTS at the time of this release which is version v8
  • Updated dependencies, base files, and editions using boundation

v5.1.0 2018 January 25

v5.0.0 2018 January 25

  • Now uses CoffeeScript v2
  • Now uses editions to use the ESNext compiled edition for environments that support it, otherwise use the ES2015 compiled edition
  • Updated base files

v4.1.0 2017 April 10

  • cson2json and json2cson will now output the error if the input file is invalid
  • Updated base files

v4.0.0 2016 October 20

  • parseCSString no longer creates the sandbox variable if it was missing, the responsibility of such things should be, and now is, handled via the eval function of coffeescript which is what that method uses - This is a major breaking change as parsing coffeescript files will now by default run still in a virtual machine but now in the global context rather than their own context - If you are parsing untrusted coffeescript files, you should now setup the sandbox option yourself with the appropriate values - If you are parsing trusted coffeescript files, or not parsing coffeescript files (e.g. only CSON files), then this change won't impact you - This change is done such that the result object from parseCSString passes result.__proto__ === Object.prototype which before it did not as the different context caused the prototype to point to a different contexts Object causing the assertion to fail
  • Updated dependencies
  • Updated internal packing conventions

v3.0.2 2015 September 18

  • Updated dependencies to avoid duplicate CoffeeScript installations

v3.0.1 2015 March 16

  • Fixed stdin support on Node 0.8

v3.0.0 2015 March 16

  • Every function now also supports callbacks (2nd or 3rd argument)
  • Errors will now always maintain their stacks where possible
  • Simplified some aliases (b/c break) - Changed stringify to now accept the arguments you would expect stringify(data, replacer, indent) - Changed parse to delegate to parseCSONString instead of parseString - Changed load to delegate to parseCSONFile instead of parseFile - Removed require (it use to delegate to requireFile)
  • Updated dependencies

v2.0.0 2015 February 6

  • API has been rewritten to be more robust and simple
  • CSON data is now parsed and stringified with the cson-parser package
  • CLI now supports stdin input
  • Node v0.11 and IO.js support

v1.6.2 2014 December 11

  • Updated dependencies

v1.6.1 2014 August 3

  • Updated dependencies

v1.6.0 2014 May 17

  • Updated dependencies
  • Fixed error handling in certain conditions

v1.5.0 2014 February 8th

  • Updated dependencies

v1.4.5 2013 October 31

  • Updated dependencies

v1.4.4 2013 August 30

  • Updated dependencies

v1.4.3 2013 August 30

  • Better error handling when requiring a file that has syntax errors
  • Fixed stringify of '{}' giving '{{}}' which is invalid - Closes issue #21

v1.4.2 2013 June 7

  • Updated dependencies

v1.4.1 2013 March 16

  • Added npm-shrinkwrap.json that ensures js2coffee uses coffee-script 1.4.0
  • Updated dependencies

v1.4.0 2012 October 25

  • Dropped require extensions following CoffeeScript's lead - If you still want them, add them to your application manually
  • Updated dependencies - coffee-script 1.3.x to 1.4.x

v1.3.0 2012 September 1

  • You can now use require to require CSON files - Thanks to Linus G Thiel for pull request #16
  • Drops node v0.4 support, min supported version now v0.6

v1.2.3 2012 September 1

v1.2.2 2012 August 10

  • Re-added markdown files to npm distribution as they are required for the npm website

v1.2.1 2012 July 16

  • Fixed try surrounding a next callback

v1.2.0 2012 July 7

  • CSON files are now sandboxed by default, ensuring they can't do bad stuff to your global scope
  • Added opts as the middle argument for parseFile, parseFileSync, parse, and parseSync functions - You can use this to specify sandbox: false if you do not want sandboxing on CSON files

v1.1.2 2012 June 22

  • We no longer have require cache our configuration files

v1.1.1 2012 June 21

  • Fixed main file location

v1.1.0 2012 June 21

  • Parsing file changes - If files have js or coffee extension, will try to require them - If files have json or cson extension, will try to read them - Otherwise will throw an unknown extension error
  • Moved tests from Mocha to Joe

v1.0.2 2012 May 04

  • Fixed some CSON use cases and added more unit tests

v1.0.1 2012 May 04

v1.0.0 2012 April 23

  • Updated tests
  • Updated package.json for latest npm
  • Cleaned up the code
  • CoffeeScript dependency is now local
  • Added synchronous API calls to the README
  • Stringify functions now output CSON strings, instead of JSON strings - Use JSON.stringify if you want JSON strings
  • Added json2cson and cson2json bin tools

v0.2 2011 August 10

  • Added synchronous interface thanks to clyfe - closes issue #1 and #3

v0.1 2011 June 2

  • Initial commit