pax_global_header00006660000000000000000000000064140012665330014512gustar00rootroot0000000000000052 comment=b778e2eb368a0b935e0a999f6366e9edd95e4dc7 parse-json-5.2.0/000077500000000000000000000000001400126653300135775ustar00rootroot00000000000000parse-json-5.2.0/.editorconfig000066400000000000000000000002571400126653300162600ustar00rootroot00000000000000root = 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.2.0/.gitattributes000066400000000000000000000000231400126653300164650ustar00rootroot00000000000000* text=auto eol=lf parse-json-5.2.0/.github/000077500000000000000000000000001400126653300151375ustar00rootroot00000000000000parse-json-5.2.0/.github/funding.yml000066400000000000000000000001641400126653300173150ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus tidelift: npm/parse-json custom: https://sindresorhus.com/donate parse-json-5.2.0/.github/security.md000066400000000000000000000002631400126653300173310ustar00rootroot00000000000000# 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.2.0/.github/workflows/000077500000000000000000000000001400126653300171745ustar00rootroot00000000000000parse-json-5.2.0/.github/workflows/main.yml000066400000000000000000000007021400126653300206420ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 14 - 12 - 10 - 8 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test parse-json-5.2.0/.gitignore000066400000000000000000000000541400126653300155660ustar00rootroot00000000000000node_modules yarn.lock .nyc_output coverage parse-json-5.2.0/.npmrc000066400000000000000000000000231400126653300147120ustar00rootroot00000000000000package-lock=false parse-json-5.2.0/index.js000066400000000000000000000024621400126653300152500ustar00rootroot00000000000000'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') }); const parseJson = (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; } }; parseJson.JSONError = JSONError; module.exports = parseJson; parse-json-5.2.0/license000066400000000000000000000021351400126653300151450ustar00rootroot00000000000000MIT 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.2.0/package.json000066400000000000000000000014711400126653300160700ustar00rootroot00000000000000{ "name": "parse-json", "version": "5.2.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.2.0/readme.md000066400000000000000000000041311400126653300153550ustar00rootroot00000000000000# 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) { if (error instanceof parseJson.JSONError) { 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?) Throws a `JSONError` when there is a parsing error. #### 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. ### parseJson.JSONError Exposed for `instanceof` checking. #### fileName Type: `string` The filename displayed in the error message. #### codeFrame Type: `string` The printable section of the JSON which produces the error. ---
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.2.0/test.js000066400000000000000000000015201400126653300151120ustar00rootroot00000000000000import 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); }); test('throws exported error error', t => { t.throws(() => { parseJson('asdf'); }, { instanceOf: parseJson.JSONError }); });