CSON vs JSON

今天在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的格式:

  1. {
  2. "greatDocumentaries": [
  3. "earthlings.com",
  4. "forksoverknives.com",
  5. "cowspiracy.com"
  6. ],
  7. "importantFacts": {
  8. "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",
  9. "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",
  10. "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",
  11. "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",
  12. "more": "http://cowspiracy.com/facts"
  13. }
  14. }

cson的格式:

  1. # Comments!!!
  2.  
  3. # An Array with no commas!
  4. greatDocumentaries: [
  5. 'earthlings.com'
  6. 'forksoverknives.com'
  7. 'cowspiracy.com'
  8. ]
  9.  
  10. # An Object without braces!
  11. importantFacts:
  12. # Multi-Line Strings! Without Quote Escaping!
  13. emissions: '''
  14. 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.
  15. Goodland, R Anhang, J. “Livestock and Climate Change: What if the key actors in climate change were pigs, chickens and cows?”
  16. WorldWatch, November/December 2009. Worldwatch Institute, Washington, DC, USA. Pp. 10–19.
  17. http://www.worldwatch.org/node/6294
  18. '''
  19.  
  20. landuse: '''
  21. Livestock covers 45% of the earth’s total land.
  22. Thornton, Phillip, Mario Herrero, and Polly Ericksen. “Livestock and Climate Change.” Livestock Exchange, no. 3 (2011).
  23. https://cgspace.cgiar.org/bitstream/handle/10568/10601/IssueBrief3.pdf
  24. '''
  25.  
  26. burger: '''
  27. One hamburger requires 660 gallons of water to produce – the equivalent of 2 months’ worth of showers.
  28. Catanese, Christina. “Virtual Water, Real Impacts.” Greenversations: Official Blog of the U.S. EPA. 2012.
  29. http://blog.epa.gov/healthywaters/2012/03/virtual-water-real-impacts-world-water-day-2012/
  30. “50 Ways to Save Your River.” Friends of the River.
  31. http://www.friendsoftheriver.org/site/PageServer?pagename=50ways
  32. '''
  33.  
  34. milk: '''
  35. 1,000 gallons of water are required to produce 1 gallon of milk.
  36. “Water trivia facts.” United States Environmental Protection Agency.
  37. http://water.epa.gov/learn/kids/drinkingwater/water_trivia_facts.cfm#_edn11
  38. '''
  39.  
  40. more: 'http://cowspiracy.com/facts'

NPM 安装方法:

  • Use: require('cson')
  • Install: npm install --save cson

使用 CSON 的方式:

1. 命令行方式:

  1. # Convert a JSON file into a CSON file
  2. json2cson in.json > out.cson
  3. # Same thing via piping
  4. cat in.json | json2cson > out.cson
  5.  
  6. # Convert a CSON file into a JSON file
  7. cson2json in.cson > out.json
  8. # Same thing via piping
  9. cat in.cson | cson2json > out.json

需要一个全局的 CSON 安装包: npm install -g cson

2. 通过 API 方式:

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

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

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

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

创建字符串:

解析字符串:

需求文件:

许可授权:

Unless stated otherwise all works are:

and licensed under:

 

CSON vs JSON的更多相关文章

  1. Confman - 针对「Node 应用」的配置文件加载模块

    一句话介绍 confman 是一个强大的配置文件加载器,无论你喜欢 yaml .cson.json.properties.plist.ini.toml.xml 还是 js,都能满足你的愿望,并且更加简 ...

  2. 使用TSQL查询和更新 JSON 数据

    JSON是一个非常流行的,用于数据交换的文本数据(textual data)格式,主要用于Web和移动应用程序中.JSON 使用“键/值对”(Key:Value pair)存储数据,能够表示嵌套键值对 ...

  3. 【疯狂造轮子-iOS】JSON转Model系列之二

    [疯狂造轮子-iOS]JSON转Model系列之二 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇<[疯狂造轮子-iOS]JSON转Model系列之一> ...

  4. 【疯狂造轮子-iOS】JSON转Model系列之一

    [疯狂造轮子-iOS]JSON转Model系列之一 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 之前一直看别人的源码,虽然对自己提升比较大,但毕竟不是自己写的,很容易遗 ...

  5. Taurus.MVC 2.2 开源发布:WebAPI 功能增强(请求跨域及Json转换)

    背景: 1:有用户反馈了关于跨域请求的问题. 2:有用户反馈了参数获取的问题. 3:JsonHelper的增强. 在综合上面的条件下,有了2.2版本的更新,也因此写了此文. 开源地址: https:/ ...

  6. .NET Core系列 : 2 、project.json 这葫芦里卖的什么药

    .NET Core系列 : 1..NET Core 环境搭建和命令行CLI入门 介绍了.NET Core环境,本文介绍.NET Core中最重要的一个配置文件project.json的相关内容.我们可 ...

  7. 一个粗心的Bug,JSON格式不规范导致AJAX错误

    一.事件回放  今天工作时碰到了一个奇怪的问题,这个问题很早很早以前也碰到过,不过没想到过这么久了竟然又栽在这里. 当时正在联调一个项目,由于后端没有提供数据接口,于是我直接本地建立了一个 json ...

  8. JSON.parse()和JSON.stringify()

    1.parse 用于从一个字符串中解析出json 对象.例如 var str='{"name":"cpf","age":"23&q ...

  9. json与JavaScript对象互换

    1,json字符串转化为JavaScript对象: 方法:JSON.parse(string) eg:var account = '{"name":"jaytan&quo ...

随机推荐

  1. Flask之静态文件处理

    静态文件的处理 推荐 from flask import Flask,render_template app = Flask(__name__,template_folder='templates', ...

  2. 虚拟化kvm的搭建

            虚拟化, 是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机 ,在一台计算机上同时运行多个逻辑计算机,每台逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立的空间内运行而互不 ...

  3. 《Effective C#》之减少装箱和拆箱

    <Effective C#>之减少装箱和拆箱_天极网 http://dev.yesky.com/msdn/359/3486359.shtml <Effective C#>之减少 ...

  4. TypeScript基本类型

    类型注解 作用:相当于强类型语言中的类型声明 语法:(变量/函数):type 数据类型 新建src/datatype.ts,里面定义各种类型的数据 原始类型: let bool: boolean = ...

  5. 消息中间件——rocketmq环境配置

    产生原因 RocketMQ概述 RocketMQ 是一款分布式.队列模型的消息中间件,具有以下特点: 能够保证严格的消息顺序 提供丰富的消息拉取模式 高效的订阅者水平扩展能力 实时的消息订阅机制 亿级 ...

  6. 【译】.NET 5. 0 中 Windows Form 的新特性

    自从 Windows Form 在 2018 年底开源并移植到 .NET Core 以来,团队和我们的外部贡献者都在忙于修复旧的漏洞和添加新功能.在这篇文章中,我们将讨论 .NET 5.0 中 Win ...

  7. Python3爬取猫眼电影信息

    Python3爬取猫眼电影信息 import json import requests from requests.exceptions import RequestException import ...

  8. 封装SpringJdbcTemplate

    package com.jy.modules.cms.query; import java.util.List; import java.util.Map; public interface quer ...

  9. 分析 BAT 互联网巨头在大数据方向布局及大数据未来发展趋势

    > 风起云涌的大数据战场上,早已迎百花齐放繁荣盛景,各大企业加速跑向"大数据时代".而我们作为大数据的践行者,在这个"多智时代"如何才能跟上大数据的潮流, ...

  10. Codeforces Round #651 (Div. 2) E. Binary Subsequence Rotation(dp)

    题目链接:https://codeforces.com/contest/1370/problem/E 题意 给出两个长为 $n$ 的 $01$ 串 $s$ 和 $t$,每次可以选择 $s$ 的一些下标 ...