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

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 7.1.0 to 7.1.1

69

index.js

@@ -11,41 +11,56 @@ import errorEx from 'error-ex';

const generateCodeFrame = (string, location, highlightCode = true) =>
codeFrameColumns(string, {start: location}, {highlightCode});
const getErrorLocation = (string, message) => {
const match = message.match(/in JSON at position (?<index>\d+)(?: \(line (?<line>\d+) column (?<column>\d+)\))? while parsing/);
if (!match) {
return;
}
let {index, line, column} = match.groups;
if (line && column) {
return {line: Number(line), column: Number(column)};
}
({line, column} = new LinesAndColumns(string).locationForIndex(Number(index)));
return {line: line + 1, column: column + 1};
};
export default function parseJson(string, reviver, filename) {
if (typeof reviver === 'string') {
filename = reviver;
reviver = null;
reviver = undefined;
}
let message;
try {
try {
return JSON.parse(string, reviver);
} catch (error) {
fallback(string, reviver);
throw error;
}
return JSON.parse(string, reviver);
} catch (error) {
error.message = error.message.replace(/\n/g, '');
const indexMatch = error.message.match(/in JSON at position (\d+) while parsing/);
message = error.message;
}
const jsonError = new JSONError(error);
if (filename) {
jsonError.fileName = filename;
}
try {
fallback(string, reviver);
} catch (error) {
message = error.message;
}
if (indexMatch && indexMatch.length > 0) {
const lines = new LinesAndColumns(string);
const index = Number(indexMatch[1]);
const location = lines.locationForIndex(index);
message = message.replace(/\n/g, '');
const jsonError = new JSONError(message);
const generateCodeFrame = ({highlightCode}) => codeFrameColumns(
string,
{start: {line: location.line + 1, column: location.column + 1}},
{highlightCode},
);
if (filename) {
jsonError.fileName = filename;
}
jsonError.codeFrame = generateCodeFrame({highlightCode: true});
jsonError.rawCodeFrame = generateCodeFrame({highlightCode: false});
}
const location = getErrorLocation(string, message);
if (location) {
jsonError.codeFrame = generateCodeFrame(string, location);
jsonError.rawCodeFrame = generateCodeFrame(string, location, /* highlightCode */ false);
}
throw jsonError;
}
throw jsonError;
}
{
"name": "parse-json",
"version": "7.1.0",
"version": "7.1.1",
"description": "Parse JSON with more helpful errors",

@@ -47,2 +47,4 @@ "license": "MIT",

"nyc": "^15.1.0",
"outdent": "^0.8.0",
"strip-ansi": "^7.1.0",
"tsd": "^0.28.1",

@@ -49,0 +51,0 @@ "xo": "^0.54.0"

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