Skip to content

Commit 4f1b0e9

Browse files
authored
Added JSON.EXCISE (#14)
* Added `JSON.EXCISE` return value for replacers and revivers, allowing array items to be conveniently deleted along with their slot in an array. * Smarter array item deletion via replacers and revivers if those functions manipulate parent array sizes. * Correct line counting with mixed line ending formats (relevant for how the locations of errors are reported).
1 parent 4c2fa02 commit 4f1b0e9

17 files changed

Lines changed: 258 additions & 163 deletions

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
### 5.2.0
2+
3+
* Added `JSON.EXCISE` return value for replacers and revivers, allowing array items to be conveniently deleted along with their slot in an array.
4+
* Smarter array item deletion via replacers and revivers if those functions manipulate parent array sizes.
5+
* Correct line counting with mixed line ending formats (relevant for how the locations of errors are reported).
6+
17
### 5.1.0
28

39
* Approximately 3–4 times faster parsing than 5.0.x versions.
@@ -39,7 +45,7 @@ Minor documentation tweak.
3945
### 4.0.0
4046

4147
* Added complete grammar documentation.
42-
* Support for older versions of JavaScript which do not handle BigInt dropped.
48+
* Support for older versions of JavaScript, which do not handle BigInt, dropped.
4349
* Related API for setting an external BigInt handler removed.
4450
* Fixed-precision decimal numbers are now explicitly IEEE 754 Decimal128 numbers.
4551
* Nomenclature to distinguish between fixed-precision and arbitrary-precision decimals has changed. It's now simply Decimal or BigDecimal rather than FixedBigDecimal or BigDecimal.

GRAMMAR.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ comment (* JSONC *) = block-comment | line-comment ;
187187
188188
block-comment = "/*", ? any characters not containing the sequence "*/" ?, "*/";
189189
190-
line-comment = "//", ? any characters other than \n or \r ?, ( "\n" | "\r\n" | "\r" ) ;
190+
line-comment = "//", ? any characters other than \n, \r, \u2028, or \u2029 ?, ( "\n" | "\r\n" | "\r" | ? LINE SEPARATOR ? (* JSON5 *) | ? PARAGRAPH SEPARATOR ? (* JSON5 *) ) ;
191191
192192
@endebnf
193193
```

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ The following features, which are not supported in standard JSON, have been adde
9393
- When a replacer function returns `undefined` for the value of an item, for consistency with JSON replacer functions, that with cause the item to be deleted from the result. `JSONZ.DELETE` can also be used for this purpose, and *is the only way to delete an object or array item with a value that is originally `undefined`.*
9494
- Since JSON-Z can handle explicit `undefined` values, a replacer function can return the special value `JSONZ.UNDEFINED` to replace an item’s value with `undefined`.
9595
- Replacer functions can return the special value `JSONZ.DELETE` to indicate that a slot in an array be left empty, creating a sparse array.
96+
- Replacer functions can return the special value `JSONZ.EXCISE` to indicate that a slot in an array should be deleted, removing the value and making the parent array length smaller as a result.
9697
- A global replacer function can be specified.
9798
- For the benefit of anonymous (arrow) functions, which do not have their own `this` as `functions` do, replacer functions are passed the holder of a key/value pair as a third argument to the function.
9899
- A replacer can return `JSONZ.LITERALLY_AS(`*string-value*`)` to specify [exactly how a given value will be stringified](#jsonzliterally_as).
@@ -101,6 +102,7 @@ The following features, which are not supported in standard JSON, have been adde
101102

102103
- A global reviver function can be specified.
103104
- For the benefit of anonymous (arrow) functions, which do not have their own `this`, reviver functions are passed the holder of a key/value pair as, or along with, the third argument to the function.
105+
- Just as described above for replacer functions, reviver function can return the special values `JSONZ.DELETE`, `JSONZ.EXCISE`, and `JSONZ.UNDEFINED`.
104106

105107
### Extended types (JSON-Z specific)
106108

@@ -217,9 +219,11 @@ The third argument passed to a JSON-Z reviver *might* be different from what a `
217219
> - `extra`: This is either a `context` object containing a `source` string, as described at the link above, or the holder of the value, i.e., the object or array, if any, which contains the given key/value pair.
218220
> - `noContext`: if `true`, `extra` is the `holder` object or array which contains the current key/value pair. Otherwise, `value` is a primitive value, and `extra` functions like the (nearly) standard `context` object, containing a `source` string, but also containing a `holder` value as well.
219221
>
220-
> Returns: Either the original `value`, `JSONZ.DELETE`, or a modified value (using `JSONZ.UNDEFINED` to change a value to `undefined`).
222+
> Returns: Either the original `value`, `JSONZ.DELETE`, `JSONZ.EXCISE`, or a modified value (using `JSONZ.UNDEFINED` to change a value to `undefined`).
221223
222-
Please note that if you want to shrink the size of a containing array when using a reviver to delete an array element, you must both perform `holder.splice(parseInt(key), 1)` within the replacer and then return `JSONZ.DELETE` from the replacer function. Returning `JSONZ.DELETE` alone will simply result in a sparse array with an empty slot.
224+
Returning `JSONZ.DELETE` for an item in an array will result in a sparse array with an empty slot, with the length of the array _remaining the same_.
225+
226+
Returning `JSONZ.EXCISE` for an item in an array will delete the corresponding slot in the parent array, with the length of the array _becoming shorter by 1_.
223227

224228
#### Return value
225229

@@ -244,7 +248,7 @@ This works very much like [`JSON.stringify`](https://developer.mozilla.org/en-US
244248
When using the standard `JSON.stringify()`, a replacer function is called with two arguments: `key` and `value`. JSON-Z adds a third argument, `holder`. This value is already available to standard replacer `function`s as `this`, but `this` won't be bound the holder when using an anonymous (arrow) function as a replacer. The JSON-Z third argument (which can be ignored if not needed) provides alternative access to the holder value.<br><br>
245249
> `replacer(key, value[, holder])`
246250
247-
<br>Please note that if you want to shrink the size of a containing array when using a replacer to delete an array element, you must both perform `‑‑holder.length` within the replacer and then return `JSONZ.DELETE` from the replacer function. Returning `JSONZ.DELETE` alone will simply result in a sparse array with an empty slot.
251+
<br>Please note that if you want to shrink the size of a parent array when using a replacer to delete an array element, return `JSONZ.EXCISE`. If you return `JSONZ.DELETE` from the replacer function, the result will be a sparse parent array, retaining its original length and containing an empty slot.
248252

249253
- `space`: A string or number used to insert whitespace into the output JSON-Z string for readability purposes. If this is a number, it indicates the number of space characters to use as whitespace; this number is capped at 10. Values less than 1 indicate that no space should be used. If `space` is a string, that string (or the first 10 characters of the string if it's longer) is used as white space. A single space adds white space without adding indentation. If this parameter is not provided (or is null), no whitespace is added. If indenting white space is used, trailing commas can optionally appear in objects and arrays.
250254
- `options`: This can either be an `OptionSet` value (see [below](#jsonzsetoptionsoptions-additionaloptions)), or an object with the following properties:
@@ -423,7 +427,11 @@ This restores all built-in type handlers, leaving any user-added type handlers.
423427

424428
### JSONZ.DELETE
425429

426-
Return this value from a replacer function to delete an item from an object or to render a slot in an array as empty.
430+
Return this value from a replacer or reviver function to delete an item from an object or to render a slot in an array as empty, not modifying the length of an array.
431+
432+
### JSONZ.EXCISE
433+
434+
Return this value from a replacer or reviver function to delete a slot in an array, making the array shorter by 1.
427435

428436
### JSONZ.UNDEFINED
429437

docs/comments.png

10.7 KB
Loading

lib/bignumber-util.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { JsonZOptions, Quote } from './options-manager';
22

3-
export function getBigDecimalType(): string;
4-
export function getDecimalType(): string;
53
export function hasBigDecimal(): boolean;
64
export function hasDecimal(): boolean;
75
export function isBigDecimal(value: unknown): boolean;

lib/bignumber-util.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ module.exports = {
5757

5858
hasBigDecimal() { return hasBigDecimal; },
5959

60-
getBigDecimalType() { return hasBigDecimal ? 'bigdecimal' : 'number'; },
61-
62-
getDecimalType() { return hasDecimal ? 'decimal' : 'number'; },
63-
6460
setDecimal(decimalClass) {
6561
hasDecimal = !!decimalClass;
6662
decimal = decimalClass;

lib/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export {
66
} from './options-manager';
77
export { parse } from './parse';
88
export { stringify } from './stringify';
9-
export { DELETE, LITERALLY_AS, UNDEFINED } from './util';
9+
export { DELETE, EXCISE, LITERALLY_AS, UNDEFINED } from './util';
1010

1111
declare const JSONZ;
1212

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const JSONZ = {
1919

2020
LITERALLY_AS: util.LITERALLY_AS,
2121
DELETE: util.DELETE,
22+
EXCISE: util.EXCISE,
2223
UNDEFINED: util.UNDEFINED,
2324

2425
addTypeHandler: options.addTypeHandler,

0 commit comments

Comments
 (0)