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
Added maxIndent, noIndent, and propertyFilter stringification options. (#15)
* Added `maxIndent`, `noIndent`, and `propertyFilter` stringification options.
* Added `context` callback parameter for both replacers and revivers with `context.holder` and `context.stack` values.
* The above results in a breaking change from 5.x versions of JSON-Z, where the third parameter of a replacer/reviver callback _might_ be expected, _depending on context_, to be a holder object. This change results in a more consistent callback paradigm.
Copy file name to clipboardExpand all lines: CHANGELOG.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,9 @@
1
+
### 6.0.0
2
+
3
+
* Added `maxIndent`, `noIndent`, and `propertyFilter` stringification options.
4
+
* Added `context` callback parameter for both replacers and revivers with `context.holder` and `context.stack` values.
5
+
* The above results in a breaking change from 5.x versions of JSON-Z, where the third parameter of a replacer/reviver callback _might_ be expected, _depending on context_, to be a holder object. This change results in a more consistent callback paradigm.
6
+
1
7
### 5.2.0
2
8
3
9
* Added `JSON.EXCISE` return value for replacers and revivers, allowing array items to be conveniently deleted along with their slot in an array.
Copy file name to clipboardExpand all lines: README.md
+14-9Lines changed: 14 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ JSON-Z is a superset of [JSON] (and of [JSONC] and [JSON5] as well) which aims t
12
12
13
13
JSON-Z is designed to increase flexibility when parsing while, by default, maintaining maximum compatibility with standard JSON when stringifying data (unless the user, through optional settings, eschews this compatibility).
14
14
15
-
JSON-Z output, like JSON and JSON5, is also valid JavaScript (with two *optional* exceptions).
15
+
JSON-Z output, like JSON and JSON5, is also valid JavaScript (with two *optional* exceptions). JSON-Z output can be valid JSON with the right optional setttings.
16
16
17
17
Even when the additional grammar features of JSON-Z are not needed, this library's replacer functions, reviver functions, and formatting capabilities provide additional capabilities which can be useful when dealing JSON and JSON5.
18
18
@@ -210,14 +210,16 @@ This works very much like [`JSON.parse`](https://developer.mozilla.org/en-US/doc
210
210
211
211
A JSON-Z reviver function is a callback that works much like the not-quite-yet-standard `JSON.parse` reviver function from this proposal: https://github.com/tc39/proposal-json-parse-with-source, a proposal which has already been widely implemented.
212
212
213
-
The third argument passed to a JSON-Z reviver *might* be different from what a `JSON.parse` reviver receives, according to the above proposal. There is a forth argument that clarifies the difference.
213
+
The `context` argument passed to a JSON-Z reviver differs in that in provides two extra values described below.
214
214
215
-
> `reviver(key, value[, extra[, noContext]])`
215
+
> `reviver(key, value[, context])`
216
216
>
217
217
> -`key`: The object key (or array index) of the value being parsed. The `key` is an empty string if the `value` is the root value.
218
218
> -`value`: A value as originally parsed, which should be returned by the reviver as-is if the reviver is not modifying the original value.
219
-
> -`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.
220
-
> -`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.
219
+
> -`context`:
220
+
> -`context.source`: A `source` string, as described at the link above, providing the original text from which a primitive value has been parsed. No `source` is provided for object or array values.
221
+
> -`context.holder`: The object or array, if any, which contains a given key/value pair.
222
+
> -`context.stack`: An array containing the heirarchy of keys/array indices leading up to the given value. For example, when parsing `"[0, 1, {foo: 77}]"`, and a reviver receives the value `77`, `context.stack` will be `['2', 'foo']`.
221
223
>
222
224
> Returns: Either the original `value`, `JSONZ.DELETE`, `JSONZ.EXCISE`, or a modified value (using `JSONZ.UNDEFINED` to change a value to `undefined`).
223
225
@@ -245,18 +247,21 @@ This works very much like [`JSON.stringify`](https://developer.mozilla.org/en-US
245
247
-`value`: The value to convert to a JSON-Z string.
246
248
-`replacer`: A function which alters the behavior of the stringification process, or an array of String and Number objects that serve as an allowlist for selecting/filtering the properties of the value object to be included in the JSON-Z string. If this value is null or not provided, all properties of the object are included in the resulting JSON-Z string.
247
249
248
-
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>
249
-
> `replacer(key, value[, holder])`
250
+
When using the standard `JSON.stringify()`, a replacer function is called with two arguments: `key` and `value`. JSON-Z adds a third argument, `context`. The `context` argument here (which can be dropped if not needed) is similar to the JSON-Z reviver `context`argument, albeit with no `context.source` value.<br><br>
251
+
> `replacer(key, value[, context])`
250
252
251
253
<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.
252
254
253
255
-`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.
254
256
-`options`: This can either be an `OptionSet` value (see [below](#jsonzsetoptionsoptions-additionaloptions)), or an object with the following properties:
255
257
-`extendedPrimitives`: If `true` (the default is `false`) this enables direct stringification of `Infinity`, `-Infinity`, `NaN`, and `undefined`. Otherwise, these values become `null`.
256
258
-`extendedTypes`: If `JSONZ.ExtendedTypeMode.AS_FUNCTIONS` or `JSONZ.ExtendedTypeMode.AS_OBJECTS` (the default is `JSONZ.ExtendedTypeMode.OFF`), this enables special representation of additional data types, such as `_Date("2019-07-28T08:49:58.202Z")`, which can be parsed directly as a JavaScript `Date` object, or `{"_$_": "Date", "_$_value": "2019-07-28T08:49:58.202Z"}`, which can be automatically rendered as a `Date` object by a built-in replacer.
257
-
-`primitiveBigDecimal`: 🧪 If `true` (the default is `false`) this enables direct stringification of arbitrary-precision big decimals using the '`m`' suffix. Otherwise, big decimals must be provided as quoted strings or extended types. _(Note: The '`m`' suffix can't be parsed as current valid JavaScript, but it is potentially a future valid standard notation.)_
259
+
-`maxIndent`: If a non-zero integer, this option limits levels of indentation, deeper than which object content will be rendered on a single line.
260
+
-`oneLiners`: An list of property names, the values for which should be rendered on a single line. This argument can be provided as an array, a `Set`, or a comma-delimited string of property names.
261
+
-`primitiveBigDecimal`: 🧪 If `true` (the default is `false`) this enables direct stringification of arbitrary-precision big decimals using the '`m`' suffix. Otherwise, big decimals must be provided as quoted strings or extended types. _(Note: The '`m`' suffix cannot be parsed as valid JavaScript, making this notation a deviation from JavaScript parsing compatibility.)_
258
262
-`primitiveBigInt`: If `true` (the default is `false`) this enables direct stringification of big integers using the '`n`' suffix. Otherwise, big integers are provided as quoted strings or extended types.
259
-
-`primitiveDecimal`: 🧪 If `true` (the default is `false`) this enables direct stringification of fixed-precision big decimals using the '`d`' suffix. Otherwise, big decimals must be provided as quoted strings or extended types. _(Note: The '`d`' suffix can't be parsed as current valid JavaScript, but it is potentially a future valid standard notation.)_
263
+
-`primitiveDecimal`: 🧪 If `true` (the default is `false`) this enables direct stringification of fixed-precision big decimals using the '`d`' suffix. Otherwise, big decimals must be provided as quoted strings or extended types. _(Note: The '`d`' suffix cannot be parsed as valid JavaScript, making this notation a deviation from JavaScript parsing compatibility.)_
264
+
-`propertyFilter`: This option provides functionality identical to providing an array of property names for the `replacer`/`options` argument — only the object properties listed here will be rendered. Using this option allows additional options to be used simultaneously.
260
265
-`quote`: A string representing the quote character to use when serializing strings (single quote `'` or double quote `"`), or one of the following values:
261
266
-`JSONZ.Quote.DOUBLE`: Always quote with double quotes (this is the default).
262
267
-`JSONZ.Quote.SINGLE`: Always quote with single quotes.
0 commit comments