You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Replace the parser generated by Jison with a hand-built parser from JJU
BREAKING CHANGE: There is no `yy.parseError` to intercept error handling. Use the thrown error - it contains all available information. The error does not include the `hash` object with structured information. Look for the [documentd properties](/prantlf/jsonlint#error-handling). The location of the error occurrence is available as `location.start`, for example.
DEPRECATION: The only exposed object to use from now on is the `parse` method as a named export. Other exports (`parser` and `Parser`) are deprecated and will be removed in future.
The parser from ["Utilities to work with JSON/JSON5 documents"](/rlidwka/jju) is four times faster, than the prevous one, has approximatly the same size and can be easier enhanced, regarding both features and error handling.
Copy file name to clipboardExpand all lines: README.md
+36-22Lines changed: 36 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -17,9 +17,9 @@ This is a fork of the original package with the following enhancements:
17
17
* Can parse and skip JavaScript-style comments.
18
18
* Can accept single quotes (apostrophes) as string delimiters.
19
19
* Implements JavaScript modules using [UMD](https://github.com/umdjs/umd) to work everywhere.
20
-
*Can use the native JSON parser to gain the [best performance](./benchmarks#json-parser-comparison), while showing error messages of the same quality.
20
+
*Prefers using the native JSON parser to gain the [best performance](./benchmarks#json-parser-comparison), while showing error messages of the same quality.
21
21
* Depends on up-to-date npm modules with no installation warnings.
Parsing methods return the parsed object or throw an error. If the data cam be parsed, you will be able to validate them against a JSON schema in addition:
94
+
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:
Compiling JSON schema supports the same options for customisation and performance improvement as parsing JSON data (`ignoreComments`, `allowSingleQuotedStrings`, `limitedErrorInfo`). 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:
104
+
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:
constvalidate=validator.compile('string with JSON schema', {
109
-
limitedErrorInfo:true,
107
+
constvalidate=compile('string with JSON schema', {
110
108
environment:'json-schema-draft-04'
111
109
})
112
-
validate(jsonData)
113
110
```
114
111
115
112
### Performance
116
113
117
114
This is a part of an output from the [parser benchmark](./benchmarks#json-parser-comparison), when parsing a 4.2 KB formatted string ([package.json](./package.json)) with Node.js 10.15.3:
118
115
119
116
the built-in parser x 61,588 ops/sec ±0.75% (80 runs sampled)
120
-
the pure jison parser x 2,516 ops/sec ±1.31% (84 runs sampled)
121
-
the extended jison parser x 2,434 ops/sec ±0.74% (89 runs sampled)
117
+
the pure jju parser x 11,396 ops/sec ±1.05% (86 runs sampled)
118
+
the extended jju parser x 8,221 ops/sec ±0.99% (87 runs sampled)
119
+
120
+
A custom JSON parser is [a lot slower](./benchmarks/results/performance.md#results) than the built-in one. However, it is more important to have a [clear error reporting](./benchmarks/results/errorReportingQuality.md#results) than the highest speed in scenarios like parsing configuration files. Extending the parser with the support for comments and single-quoted strings does not affect significantly the performance.
121
+
122
+
### Error Handling
123
+
124
+
If parsing fails, a `SyntaxError` will be thrown with the following properties:
|`exzerpt`| part of the input string around the error |
131
+
|`pointer`| "--^" pointing to the error in `exzerpt`|
132
+
|`location`| object pointing to the error location |
133
+
134
+
The `location` object contains properties `line`, `column` and `offset`.
122
135
123
-
The custom JSON parser is [a lot slower](./benchmarks/results/performance.md#results) than the built-in one. However, it is more important to have a [clear error reporting](./benchmarks/results/errorReportingQuality.md#results) than the highest speed in scenarios like parsing configuration files. Extending the parser with the support for comments and single-quoted strings does not affect significantly the performance.
136
+
The following code logs twice the following message:
124
137
125
-
You can enable the (fastest) native JSON parser by the `limitedErrorInfo` option, if you do not need the full structured error information provided by the custom parser. The error thrown by the native parser includes the same rich message, but some properties are missing, because the native parser does not return structured information. Parts of the message are returned instead to allow custom error reporting:
0 commit comments