Socket
Socket
Sign inDemoInstall

json-parser

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-parser - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

4

package.json
{
"name": "json-parser",
"version": "0.1.0",
"description": "JSON parser to parse the AST of JSON object even with comments.",
"version": "0.1.1",
"description": "JSON parser to parse JSON object and MAINTAIN comments.",
"main": "index.js",

@@ -6,0 +6,0 @@ "scripts": {

# json-parser [![NPM version](https://badge.fury.io/js/json-parser.svg)](http://badge.fury.io/js/json-parser) [![Build Status](https://travis-ci.org/kaelzhang/node-json-parser.svg?branch=master)](https://travis-ci.org/kaelzhang/node-json-parser) [![Dependency Status](https://gemnasium.com/kaelzhang/node-json-parser.svg)](https://gemnasium.com/kaelzhang/node-json-parser)
JSON parser to parse the AST of JSON object even with comments.
JSON parser to parse JSON object and MAINTAIN comments.

@@ -13,8 +13,45 @@ ## Install

content
```
/**
blah
*/
// comment at top
{
// comment for a
/* block comment */
"a": 1 // comment at right
}
// comment at bottom
```
```js
var json_parser = require('json-parser');
var parser = require('json-parser');
var object = parser.parse(content);
console.log(object);
```
And the result will be:
```js
{
// Comments at the top of the file
'//^': ['/**\n blah\n */', '// comment at top'],
// Comments at the bottom of the file
'//$': ['// comment at bottom'],
// Comment for a property is the value of `'// <prop>'`
'// a': [
['// comment for a', '/* block comment */'],
['// comment at right']
],
// The real value
a: 1
}
```
## License
MIT
'use strict';
var expect = require('chai').expect;
var json_parser = require('../');
var parser = require('../');
// var a = parser.parse('//top\n{// top a\n/* abc */"a":1,//right\n/* bcd */"b":{"a":1}}//bottom');
// // var a = parser.parse('{/*top*/"a":1,//right\n/*abc*/"b":{"a":1}}');
// console.log(a);
var cases = [
{
d: 'comment at the top',
s: '//top\n{"a":1}',
e: function (obj) {
expect(obj.a).to.equal(1);
expect(obj['//^']).to.deep.equal(['//top']);
}
},
{
d: 'multiple comments at the top, both line and block',
s: '//top\n/*abc*/{"a":1}',
e: function (obj) {
expect(obj.a).to.equal(1);
expect(obj['//^']).to.deep.equal(['//top', '/*abc*/']);
}
},
{
d: 'comment at the bottom',
s: '{"a":1}\n//bot',
e: function (obj) {
expect(obj.a).to.equal(1);
expect(obj['//$']).to.deep.equal(['//bot']);
}
},
{
d: 'multiple comments at the bottom, both line and block',
s: '{"a":1}\n//top\n/*abc*/',
e: function (obj) {
expect(obj.a).to.equal(1);
expect(obj['//$']).to.deep.equal(['//top', '/*abc*/']);
}
},
{
d: 'comment for properties',
s: '{//a\n"a":1}',
e: function (obj) {
expect(obj.a).to.equal(1);
expect('// a' in obj).to.equal(true);
expect(obj['// a']).to.deep.equal([['//a']]);
}
},
{
d: 'comment for properties, multiple at the top',
s: '{//a\n/*b*/"a":1}',
e: function (obj) {
expect(obj.a).to.equal(1);
expect('// a' in obj).to.equal(true);
expect(obj['// a']).to.deep.equal([['//a', '/*b*/']]);
}
},
{
d: 'comment for properties, both top and right',
s: '{//a\n"a":1//b\n}',
e: function (obj) {
expect(obj.a).to.equal(1);
expect('// a' in obj).to.equal(true);
expect(obj['// a']).to.deep.equal([['//a'], ['//b']]);
}
}
]
describe("parse()", function(){
cases.forEach(function (c) {
it(c.d, function(){
c.e(parser.parse(c.s));
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc