xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

CSON vs JSON All In One

CSON vs JSON All In One

今天在 GitHub 浏览资料时,无意发现了这个很像 JSON, 却优于 JSON 的CSON
故,在此分享给大家!

官方 fork 文档:https://github.com/xgqfrms/cson

What is CSON?

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

cosn 有比 json 更宽松,更好的读写方式,并且不需要引号,摆脱一切,它拥有注释和可读的多行字符串,即使你忘了一个逗号,也不会出错!

对比

JSON 的格式:
{
  "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"
  }
}

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'

NPM 安装方法:

$ npm install --save cson

https://www.npmjs.com/package/cson

https://github.com/bevry/cson

使用 CSON 的方式:

  1. cli 命令行方式:
# 需要一个全局的 CSON 安装包
$ npm install -g cson

convert JSON into CSON

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

convert CSON into JSON

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

  1. 通过 API 方式:

每一个方法都能被执行 ,使用像这样的一个回调:

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

或, 通过像这样的一个回调:

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

创建字符串:

http://rawgit.com/bevry/cson/master/docs/classes/CSON.html#stringify-instance

String CSON.stringify(data, replacer?, indent?)
Converts an Object into a CSON String

String CSON.createCSONString(data, opts?, next?)
Converts an Object into a CSON String

String CSON.createJSONString(data, opts?, next?)
Converts an Object into a JSON String

String CSON.createString(data, opts?, next?)
Converts an Object into a String of the desired format If the format option is not specified, we default to CSON

posted @   xgqfrms  阅读(336)  评论(3编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示