Skip to content

Commit

Permalink
feat: Support reviver from the native JSON.parse method
Browse files Browse the repository at this point in the history
Documentation only. The feature was added by integrating the JJU parser (a4a606c).
  • Loading branch information
prantlf committed Jun 2, 2019
1 parent a4a606c commit 83cd33c
Showing 1 changed file with 53 additions and 14 deletions.
67 changes: 53 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,53 @@ This is a fork of the original package with the following enhancements:

* Handles multiple files on the command line (by Greg Inman).
* Walks directories recursively (by Paul Vollmer).
* Provides 100% compatible interface to the native `JSON.parse` method.
* Optionally recognizes JavaScript-style comments and single quoted strings.
* Supports JSON Schema drafts 04, 06 and 07.
* Can parse and skip JavaScript-style comments.
* Can accept single quotes (apostrophes) as string delimiters.
* Prefers the native JSON parser to gain the [best performance](./benchmarks#json-parser-comparison), while showing error messages of the same quality.
* Implements JavaScript modules using [UMD](https://github.com/umdjs/umd) to work everywhere.
* Prefers using the native JSON parser to gain the [best performance](./benchmarks#json-parser-comparison), while showing error messages of the same quality.
* Depends on up-to-date npm modules with no installation warnings.
* Small size - 17.6 kB minified, 6.1 kB gzipped.

## Synopsis

Check syntax of JSON files:

jsonlint -q data/*.json

Parse a JSON string:

```js
const { parse } = require('@prantlf/jsonlint')
const data = parse('{"creative": false}')
```

Example of an error message:

Parse error on line 1, column 14:
{"creative": ?}
-------------^
Unexpected token ?

## Command-line Interface

Install `jsonlint` with `npm`` globally to be able to use the command-line interface:
Install `jsonlint` with `npm`` globally to be able to use the command-line interface in any directory:

npm i @prantlf/jsonlint -g

Validate a file like so:
Validate a single file:

jsonlint myfile.json

or pipe the input into stdin:
or pipe the JSON input into `stdin`:

cat myfile.json | jsonlint

or process all `.json` files in a directory:

jsonlint mydir

`jsonlint` will either report a syntax error with details or pretty print the source if it is valid.
By default, `jsonlint` will either report a syntax error with details or pretty-print the source if it is valid.

### Options

Expand Down Expand Up @@ -74,24 +94,43 @@ or process all `.json` files in a directory:

Install `jsonlint` with `npm` locally to be able to use the module programmatically:

npm i @prantlf/jsonlint -D
npm i @prantlf/jsonlint -S

You might prefer methods this module to the built-in `JSON.parse` method because of a better error reporting or support for JavaScript-like comments:
The only exported item is the `parse` method, which parses a string in the JSON format to a JavaScript object, array, or value:

```js
const { parse } = require('@prantlf/jsonlint')
// Fails at the position of the character "?".
parse('{"creative": ?}') // throws an error
const data2 = parse('{"creative": ?}') // throws an error
// Succeeds returning the parsed JSON object.
parse('{"creative": false}')
const data3 = parse('{"creative": false}')
// Recognizes comments and single-quoted strings.
parse("{'creative': true /* for creativity */}", {
const data3 = parse("{'creative': true /* for creativity */}", {
ignoreComments: true,
allowSingleQuotedStrings: true
})
```

The parsing method returns the parsed object or throws an error. If the parsing succeeds, you can to validate the input against a JSON schema too:
The exported `parse` method is compatible with the native `JSON.parse` method. The second parameter provides the additional functionality:

parse(input, [reviver|options])

| Parameter | Description |
| ---------- | ------------------------------------------- |
| `input` | text in the JSON format (string) |
| `reviver` | converts object and array values (function) |
| `options` | customize parsing options (object) |

The `parse` method offers more detailed [error information](#error-handling), than the native `JSON.parse` method and it supports additional parsing options:

| Option | Description |
| -------------------------- | --------------------------- |
| `ignoreComments` | ignores single-line and multi-line JavaScript-style comments during parsing as another "whitespace" |
| `allowSingleQuotedStrings` | accepts strings delimited by single-quotes too |

### Schema Validation

The parsing method returns the parsed object or throws an error. If the parsing succeeds, you can validate the input against a JSON schema using the `lib/validator` module:

```js
const { parse } = require('@prantlf/jsonlint')
Expand All @@ -101,7 +140,7 @@ const validate = compile('string with JSON schema')
validate(parse('string with JSON data'))
```

Compiling JSON schema supports the same options as parsing JSON data (`ignoreComments`, `allowSingleQuotedStrings`). They can be passed as the second (object) parameter. The optional second `environment` parameter can be passed as an additional property in the options object too:
Compiling JSON schema supports the same options as parsing JSON data (except for `reviver`). They can be passed as the second (object) parameter. The optional second `environment` parameter can be passed either as a string or as an additional property in the options object too:

```js
const validate = compile('string with JSON schema', {
Expand Down

0 comments on commit 83cd33c

Please sign in to comment.