Socket
Socket
Sign inDemoInstall

parse-json

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parse-json - npm Package Compare versions

Comparing version 4.0.0 to 5.0.0

41

index.js
'use strict';
const errorEx = require('error-ex');
const fallback = require('json-parse-better-errors');
const {default: LinesAndColumns} = require('lines-and-columns');
const {codeFrameColumns} = require('@babel/code-frame');
const JSONError = errorEx('JSONError', {
fileName: errorEx.append('in %s')
fileName: errorEx.append('in %s'),
codeFrame: errorEx.append('\n\n%s\n')
});
module.exports = (input, reviver, filename) => {
module.exports = (string, reviver, filename) => {
if (typeof reviver === 'string') {

@@ -17,18 +20,32 @@ filename = reviver;

try {
return JSON.parse(input, reviver);
} catch (err) {
fallback(input, reviver);
throw err;
return JSON.parse(string, reviver);
} catch (error) {
fallback(string, reviver);
throw error;
}
} catch (err) {
err.message = err.message.replace(/\n/g, '');
} catch (error) {
error.message = error.message.replace(/\n/g, '');
const indexMatch = error.message.match(/in JSON at position (\d+) while parsing near/);
const jsonErr = new JSONError(err);
const jsonError = new JSONError(error);
if (filename) {
jsonErr.fileName = filename;
jsonError.fileName = filename;
}
throw jsonErr;
if (indexMatch && indexMatch.length > 0) {
const lines = new LinesAndColumns(string);
const index = Number(indexMatch[1]);
const location = lines.locationForIndex(index);
const codeFrame = codeFrameColumns(
string,
{start: {line: location.line + 1, column: location.column + 1}},
{highlightCode: true}
);
jsonError.codeFrame = codeFrame;
}
throw jsonError;
}
};
{
"name": "parse-json",
"version": "4.0.0",
"description": "Parse JSON with more helpful errors",
"license": "MIT",
"repository": "sindresorhus/parse-json",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && nyc ava"
},
"files": [
"index.js",
"vendor"
],
"keywords": [
"parse",
"json",
"graceful",
"error",
"message",
"humanize",
"friendly",
"helpful",
"string",
"str"
],
"dependencies": {
"error-ex": "^1.3.1",
"json-parse-better-errors": "^1.0.1"
},
"devDependencies": {
"ava": "*",
"nyc": "^11.2.1",
"xo": "*"
}
"name": "parse-json",
"version": "5.0.0",
"description": "Parse JSON with more helpful errors",
"license": "MIT",
"repository": "sindresorhus/parse-json",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && nyc ava"
},
"files": [
"index.js",
"vendor"
],
"keywords": [
"parse",
"json",
"graceful",
"error",
"message",
"humanize",
"friendly",
"helpful",
"string"
],
"dependencies": {
"@babel/code-frame": "^7.0.0",
"error-ex": "^1.3.1",
"json-parse-better-errors": "^1.0.1",
"lines-and-columns": "^1.1.6"
},
"devDependencies": {
"ava": "^1.4.1",
"nyc": "^14.1.1",
"xo": "^0.24.0"
}
}

@@ -17,2 +17,3 @@ # parse-json [![Build Status](https://travis-ci.org/sindresorhus/parse-json.svg?branch=master)](https://travis-ci.org/sindresorhus/parse-json)

const parseJson = require('parse-json');
const json = '{\n\t"foo": true,\n}';

@@ -32,5 +33,8 @@

/*
JSONError: Trailing comma in object at 3:1
}
^
JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}'
1 | {
2 | "foo": true,
> 3 | }
| ^
*/

@@ -41,5 +45,8 @@

/*
JSONError: Trailing comma in object in foo.json:3:1
}
^
JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json
1 | {
2 | "foo": true,
> 3 | }
| ^
*/

@@ -51,10 +58,13 @@

parseJson(json);
} catch (err) {
err.fileName = 'foo.json';
throw err;
} catch (error) {
error.fileName = 'foo.json';
throw error;
}
/*
JSONError: Trailing comma in object in foo.json:3:1
}
^
JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json
1 | {
2 | "foo": true,
> 3 | }
| ^
*/

@@ -65,5 +75,5 @@ ```

### parseJson(input, [reviver], [filename])
### parseJson(string, reviver?, filename?)
#### input
#### string

@@ -86,4 +96,12 @@ Type: `string`

## License
---
MIT © [Sindre Sorhus](https://sindresorhus.com)
<div align="center">
<b>
<a href="https://tidelift.com/subscription/pkg/npm-parse-json?utm_source=npm-parse-json&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>
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