Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

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 1.1.5 to 2.0.0

HISTORY.md

34

package.json
{
"name": "json-parser",
"version": "1.1.5",
"version": "2.0.0",
"description": "JSON parser to parse JSON object and MAINTAIN comments.",
"main": "index.js",
"main": "src/index.js",
"scripts": {
"test": "./node_modules/.bin/mocha --reporter spec ./test/*.js"
"test": "NODE_DEBUG=json-parser nyc ava --timeout=10s --verbose",
"test:dev": "NODE_DEBUG=json-parser nyc ava --timeout=10s --verbose && npm run report:dev",
"lint": "eslint .",
"fix": "eslint . --fix",
"posttest": "npm run report",
"report": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
"report:dev": "nyc report --reporter=html && npm run report:open",
"report:open": "open coverage/index.html"
},
"files": [
"src/"
],
"repository": {

@@ -24,4 +34,10 @@ "type": "git",

"engines": {
"node": ">=0.10.0"
"node": ">= 6"
},
"ava": {
"babel": false,
"files": [
"test/*.test.js"
]
},
"author": "kaelzhang",

@@ -33,8 +49,12 @@ "license": "MIT",

"devDependencies": {
"mocha": "*",
"chai": "*"
"@ostai/eslint-config": "^3.2.0",
"ava": "^2.1.0",
"codecov": "^3.5.0",
"eslint": "^5.16.0",
"eslint-plugin-import": "^2.17.3",
"nyc": "^14.1.1"
},
"dependencies": {
"esprima": "^2.7.0"
"esprima": "^4.0.1"
}
}

@@ -1,4 +0,3 @@

[![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://david-dm.org/kaelzhang/node-json-parser.svg)](https://david-dm.org/kaelzhang/node-json-parser) -->
[![Coverage](https://codecov.io/gh/kaelzhang/node-json-parser/branch/master/graph/badge.svg)](https://codecov.io/gh/kaelzhang/node-json-parser)

@@ -11,6 +10,10 @@ # json-parser

## What's new in `2.0.0`
`json-parser@2.0.0` brings some breaking changes by completely refactoring with [`Symbol`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol)s
## Install
```sh
$ npm install json-parser --save
$ npm i json-parser
```

@@ -21,10 +24,12 @@

```js
parser(text, [reviver=null,] [remove_comments=false])
const {parse} = require('json-parser')
parse(text, reviver? = null, remove_comments? = false): null | object
```
- text `String` The string to parse as JSON. See the [JSON](http://json.org/) object for a description of JSON syntax.
- reviver `function()|null` Default to `null`. It acts the same as the second parameter of [`JSON.parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse). If a function, prescribes how the value originally produced by parsing is transformed, before being returned.
- remove_comments `Boolean` If true, the parsed JSON Object won't contain comments
- **text** `string` The string to parse as JSON. See the [JSON](http://json.org/) object for a description of JSON syntax.
- **reviver?** `Function() | null` Default to `null`. It acts the same as the second parameter of [`JSON.parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse). If a function, prescribes how the value originally produced by parsing is transformed, before being returned.
- **remove_comments?** `boolean = false` If true, the parsed JSON Object won't contain comments
Returns the `Object` corresponding to the given JSON text.
Returns `object | null` corresponding to the given JSON text.

@@ -35,17 +40,28 @@ content

/**
blah
before-all
*/
// comment at top
// before-all
{
// comment for a
/* block comment */
"a": 1 // comment at right
// before
/* before */
"foo" /* after-prop:foo */ : // after-comma:foo
1 // after-value:foo
// after-value:foo
, // after-comma:foo
// after-comma: foo
"bar": [ // before
// before
"baz" // after-value:0
// after-value:0
, // after-comma: 0
"quux"
// after-value:1
] // after-value:bar
// after-value:bar
}
// comment at bottom
// after-all
```
```js
var parser = require('json-parser');
var object = parser.parse(content);
console.log(object);
console.log(parse(content))
```

@@ -58,21 +74,61 @@

// Comments at the top of the file
'//^': ['/**\n blah\n */', '// comment at top'],
[Symbol.for('before-all')]: [{
type: 'BlockComment',
value: '\n before-all\n ',
inline: false
}, {
type: 'LineComment',
value: ' before-all',
inline: false
}],
...
// Comments at the bottom of the file
'//$': ['// comment at bottom'],
[Symbol.for('after-prop:foo')]: [{
type: 'BlockComment',
value: ' after-prop:foo ',
inline: true
}],
// Comment for a property is the value of `'// <prop>'`
'// a': [
['// comment for a', '/* block comment */'],
['// comment at right']
],
// The real value
a: 1
foo: 1,
bar: [
"baz",
"quux,
[Symbol.for('after-value:0')]: [{
type: 'LineComment',
value: ' after-value:0',
inline: true
}, ...],
...
]
}
```
There are **SEVEN** kinds of symbol properties:
- `Symbol.for('before-all')` comment tokens before the JSON object
- `Symbol.for('before')` comment tokens before any properties/items inside an object/array
- `Symbol.for(\`after-prop:${prop}\`)` comment tokens after property key `prop` and before colon(`:`)
- `Symbol.for(\`after-colon:${prop}\`)` comment tokens after the colon(`:`) of property `prop` and before property value
- `Symbol.for(\`after-value:${prop}\`)` comment tokens after the value of property `prop`/the item of index `prop` and before the key-value/item delimiter(`,`)
- `Symbol.for(\`after-comma:${prop}\`)` comment tokens after the comma of `prop`-value pair and before the next key-value pair/item
- `Symbol.for('after-all')` comment tokens after the JSON object
And the value of each symbol property is an **array** of `CommentToken`
```ts
interface CommentToken {
type: 'BlockComment' | 'LineComment'
// The content of the comment, including whitespaces and line breaks
value: string
// If the start location is the same line as the previous token,
// then `inline` is `true`
inline: boolean
}
```
### Parse into an object without comments
```js
var object_no_comments = parser.parse(content, null, true);
console.log(object_no_comments)
console.log(parse(content, null, true))
```

@@ -84,8 +140,74 @@

{
a: 1
foo: 1,
bar: [
"baz",
"quux"
]
}
```
### Special cases
```js
const parsed = parse(`
// comment
1
`)
console.log(parsed === 1)
// false
```
If we parse a JSON of primative type with `remove_comments:false`, then the return value of `parse()` will be of object type.
The value of `parsed` is equivalent to:
```js
const parsed = new Number(1)
parsed[Symbol.for('before-all')] = [{
type: 'LineComment',
value: ' comment',
inline: false
}]
```
Which is similar for:
- `Boolean` type
- `String` type
For example
```js
const parsed = parse(`
"foo" /* comment */
`)
```
Which is equivalent to
```js
const parsed = new String('foo')
parsed[Symbol.for('after-all')] = [{
type: 'BlockComment',
value: ' comment ',
inline: true
}]
```
But there is one exception:
```js
const parsed = parse(`
// comment
null
`)
console.log(parsed === null) // true
```
## License
MIT
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