pax_global_header00006660000000000000000000000064137202705040014512gustar00rootroot0000000000000052 comment=0661a9c246bcec65407322126cf473dda91c0727 parse-json-5.1.0/000077500000000000000000000000001372027050400135765ustar00rootroot00000000000000parse-json-5.1.0/.editorconfig000066400000000000000000000002571372027050400162570ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 parse-json-5.1.0/.gitattributes000066400000000000000000000000231372027050400164640ustar00rootroot00000000000000* text=auto eol=lf parse-json-5.1.0/.github/000077500000000000000000000000001372027050400151365ustar00rootroot00000000000000parse-json-5.1.0/.github/funding.yml000066400000000000000000000001641372027050400173140ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus tidelift: npm/parse-json custom: https://sindresorhus.com/donate parse-json-5.1.0/.github/security.md000066400000000000000000000002631372027050400173300ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. parse-json-5.1.0/.gitignore000066400000000000000000000000541372027050400155650ustar00rootroot00000000000000node_modules yarn.lock .nyc_output coverage parse-json-5.1.0/.npmrc000066400000000000000000000000231372027050400147110ustar00rootroot00000000000000package-lock=false parse-json-5.1.0/.travis.yml000066400000000000000000000000761372027050400157120ustar00rootroot00000000000000language: node_js node_js: - '14' - '12' - '10' - '8' parse-json-5.1.0/index.js000066400000000000000000000023621372027050400152460ustar00rootroot00000000000000'use strict'; const errorEx = require('error-ex'); const fallback = require('json-parse-even-better-errors'); const {default: LinesAndColumns} = require('lines-and-columns'); const {codeFrameColumns} = require('@babel/code-frame'); const JSONError = errorEx('JSONError', { fileName: errorEx.append('in %s'), codeFrame: errorEx.append('\n\n%s\n') }); module.exports = (string, reviver, filename) => { if (typeof reviver === 'string') { filename = reviver; reviver = null; } try { try { return JSON.parse(string, reviver); } catch (error) { fallback(string, reviver); throw error; } } catch (error) { error.message = error.message.replace(/\n/g, ''); const indexMatch = error.message.match(/in JSON at position (\d+) while parsing/); const jsonError = new JSONError(error); if (filename) { jsonError.fileName = filename; } 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; } }; parse-json-5.1.0/license000066400000000000000000000021351372027050400151440ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. parse-json-5.1.0/package.json000066400000000000000000000014711372027050400160670ustar00rootroot00000000000000{ "name": "parse-json", "version": "5.1.0", "description": "Parse JSON with more helpful errors", "license": "MIT", "repository": "sindresorhus/parse-json", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://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-even-better-errors": "^2.3.0", "lines-and-columns": "^1.1.6" }, "devDependencies": { "ava": "^1.4.1", "nyc": "^14.1.1", "xo": "^0.24.0" } } parse-json-5.1.0/readme.md000066400000000000000000000036241372027050400153620ustar00rootroot00000000000000# parse-json [![Build Status](https://travis-ci.com/sindresorhus/parse-json.svg?branch=master)](https://travis-ci.com/github/sindresorhus/parse-json) > Parse JSON with more helpful errors ## Install ``` $ npm install parse-json ``` ## Usage ```js const parseJson = require('parse-json'); const json = '{\n\t"foo": true,\n}'; JSON.parse(json); /* undefined:3 } ^ SyntaxError: Unexpected token } */ parseJson(json); /* JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' 1 | { 2 | "foo": true, > 3 | } | ^ */ parseJson(json, 'foo.json'); /* JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json 1 | { 2 | "foo": true, > 3 | } | ^ */ // You can also add the filename at a later point try { parseJson(json); } catch (error) { error.fileName = 'foo.json'; throw error; } /* JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json 1 | { 2 | "foo": true, > 3 | } | ^ */ ``` ## API ### parseJson(string, reviver?, filename?) #### string Type: `string` #### reviver Type: `Function` Prescribes how the value originally produced by parsing is transformed, before being returned. See [`JSON.parse` docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter ) for more. #### filename Type: `string` Filename displayed in the error message. ---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
parse-json-5.1.0/test.js000066400000000000000000000013141372027050400151120ustar00rootroot00000000000000import test from 'ava'; import parseJson from '.'; const jsonErrorRegex = /Unexpected token "}".*in foo\.json/; test('main', t => { t.truthy(parseJson('{"foo": true}')); t.throws(() => { parseJson('{\n\t"foo": true,\n}'); }, { name: 'JSONError', message: /Unexpected token "}"/ }); t.throws(() => { try { parseJson('{\n\t"foo": true,\n}'); } catch (error) { error.fileName = 'foo.json'; throw error; } }, jsonErrorRegex); t.throws(() => { parseJson('{\n\t"foo": true,\n}', 'foo.json'); }, jsonErrorRegex); t.throws(() => { try { parseJson('{\n\t"foo": true,\n}', 'bar.json'); } catch (error) { error.fileName = 'foo.json'; throw error; } }, jsonErrorRegex); });