github-pages-deploy-action/node_modules/parse-json/readme.md

102 lines
1.9 KiB
Markdown
Raw Permalink Normal View History

2020-03-07 11:45:40 +08:00
# parse-json [![Build Status](https://travis-ci.org/sindresorhus/parse-json.svg?branch=master)](https://travis-ci.org/sindresorhus/parse-json)
> Parse JSON with more helpful errors
## Install
```
2020-03-31 20:57:48 +08:00
$ npm install parse-json
2020-03-07 11:45:40 +08:00
```
## Usage
```js
2020-03-31 20:57:48 +08:00
const parseJson = require('parse-json');
const json = '{\n\t"foo": true,\n}';
2020-03-07 11:45:40 +08:00
JSON.parse(json);
/*
undefined:3
}
^
SyntaxError: Unexpected token }
*/
parseJson(json);
/*
2020-03-31 20:57:48 +08:00
JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}'
1 | {
2 | "foo": true,
> 3 | }
| ^
2020-03-07 11:45:40 +08:00
*/
parseJson(json, 'foo.json');
/*
2020-03-31 20:57:48 +08:00
JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json
1 | {
2 | "foo": true,
> 3 | }
| ^
2020-03-07 11:45:40 +08:00
*/
2020-03-31 20:57:48 +08:00
// You can also add the filename at a later point
2020-03-07 11:45:40 +08:00
try {
parseJson(json);
2020-03-31 20:57:48 +08:00
} catch (error) {
error.fileName = 'foo.json';
throw error;
2020-03-07 11:45:40 +08:00
}
/*
2020-03-31 20:57:48 +08:00
JSONError: Unexpected token } in JSON at position 16 while parsing near '{ "foo": true,}' in foo.json
1 | {
2 | "foo": true,
> 3 | }
| ^
2020-03-07 11:45:40 +08:00
*/
```
## API
2020-03-31 20:57:48 +08:00
### parseJson(string, reviver?, filename?)
2020-03-07 11:45:40 +08:00
2020-03-31 20:57:48 +08:00
#### string
2020-03-07 11:45:40 +08:00
Type: `string`
#### reviver
2020-03-31 20:57:48 +08:00
Type: `Function`
2020-03-07 11:45:40 +08:00
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.
2020-03-31 20:57:48 +08:00
---
2020-03-07 11:45:40 +08:00
2020-03-31 20:57:48 +08:00
<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>