• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

jsonicjs/jsonic: JSON parser that isn't strict

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

jsonicjs/jsonic

开源软件地址:

https://github.com/jsonicjs/jsonic

开源编程语言:

JavaScript 95.8%

开源软件介绍:

jsonic

npm version Build Status Dependency Status

A JSON parser for Node.js that isn't strict.

A JSON parser that can parse "bad" JSON. Mostly, this is about avoiding the need to quote everything!

Strict JSON requires you to do this:

{ "foo":"bar", "red":1 }

The JavaScript language itself is a little easier:

{ foo:"bar", red:1, }

But if you really want to be lazy, jsonic lets you say:

foo:bar, red:1,

See below for the relaxed JSON rules.

This module is used by the Seneca framework to provide an abbreviated command syntax.

Support

If you're using this module, feel free to contact me on twitter if you have any questions! :) @rjrodger

Quick example

var jsonic = require('jsonic')

// parse a string into a JavaScript object
var obj = jsonic('foo:1, bar:zed')

// prints { foo: 1, bar: 'zed' }
console.dir( obj )

Install

npm install jsonic

Relaxed Rules

JSONIC format is just standard JSON, with a few rule relaxations:

  • You don't need to quote property names: { foo:"bar baz", red:255 }
  • You don't need the top level braces: foo:"bar baz", red:255
  • You don't need to quote strings with spaces: foo:bar baz, red:255
  • You do need to quote strings if they contain a comma or closing brace or square bracket: icky:",}]"
  • You can use single quotes for strings: Jules:'Cry "Havoc," and let slip the dogs of war!'
  • You can have trailing commas: foo:bar, red:255,

Stringify

The jsonic module provides a stringify method:

console.log( jsonic.stringify( {a:"bc",d:1} ) ) // prints {a:bc,d:1} 

The stringify method converts a plain JavaScript object into a string that can be parsed by jsonic. It has two parameters:

  • value: plain object
  • options: optional options object

For example, you can limit the depth of the object tree printed:

console.log( jsonic.stringify( {a:{b:{c:1}}}, {depth:2} ) ) // prints {a:{b:{}}} 

NOTE: jsonic.stringify is intended for debug printing, not data exchange, so the defaults are conservative in the amount of data printed

The options are:

  • depth: default: 3; maximum depth of sub-objects printed; NOTE: there is no infinite-cycle protection, just this finite depth
  • maxitems: default: 11; maximum number of array elements or object key/value pairs printed
  • maxchars: default: 111; maximum number of characters printed
  • omit: default:[]; omit listed keys from objects
  • exclude: default:['$']; omit keys from objects if they contain any of the listed values

How it Works

The parser uses PEG.js and is an extension of the example JSON parser included in that project.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap