Skip to content

Commit e927eb8

Browse files
authored
Added a new replacer function feature, JSONZ.LITERALLY_AS. (#1)
1 parent efca0bf commit e927eb8

File tree

8 files changed

+369
-302
lines changed

8 files changed

+369
-302
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 4.1.0
2+
3+
* Added a new replacer function feature, `JSONZ.LITERALLY_AS`, allowing a replacer function to explicitly determine how a value will be rendered when stringified.
4+
15
### 4.0.1
26

37
Minor documentation tweak.

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ The following features, which are not supported in standard JSON, have been adde
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.
9696
- A global replacer function can be specified.
9797
- 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.
98+
- A replacer can return `JSONZ.LITERALLY_AS(`*string-value*`)` to specify [exactly how a given value will be stringified](#jsonzliterally_as).
9899

99100
### Reviver functions (JSON-Z specific differences)
100101

@@ -400,6 +401,31 @@ This removes all user-added type handlers, and restores all built-in type handle
400401

401402
This restores all built-in type handlers, leaving any user-added type handlers.
402403

404+
### JSONZ.DELETE
405+
406+
Return this value from a replacer function to delete an item from an object or to render a slot in an array as empty.
407+
408+
### JSONZ.UNDEFINED
409+
410+
Return this value from a replacer function (rather than returning `undefined` itself) to change a value to `undefined`.
411+
412+
### JSONZ.LITERALLY_AS()
413+
414+
Return `JSONZ.LITERALLY_AS(`*string-value*`)` from a replacer function to literally and explicitly specify how a particular value should be stringified. In this example:
415+
416+
```javascript
417+
function showNumbersInHex(k, v) {
418+
return typeof v === 'number' && isFinite(v) && !isNaN(v)
419+
? JSONZ.LITERALLY_AS((v < 0 ? '-' : '') +
420+
'0x' + Math.abs(v).toString(16).toUpperCase())
421+
: v;
422+
}
423+
424+
JSONZ.stringify({ hexValue: 912559 }, showNumbersInHex)
425+
```
426+
427+
...`stringify` with return `"{hexValue:0xDECAF}"` as a result.
428+
403429
### Node.js `require()` JSON-Z files
404430

405431
When using Node.js, you can `require()` JSON-Z files by adding the following

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const JSONZ = {
1717
setParseOptions: options.setParseOptions,
1818
resetParseOptions: options.resetParseOptions,
1919

20+
LITERALLY_AS: util.LITERALLY_AS,
2021
DELETE: util.DELETE,
2122
UNDEFINED: util.UNDEFINED,
2223

lib/stringify.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const util = require('./util');
22
const big = require('./bignumber-util');
33
const optionsMgr = require('./options-manager');
4+
const { LITERALLY_AS_CLASS } = require('./util');
45

56
const escapes = {
67
"'": "\\'",
@@ -160,6 +161,10 @@ module.exports = function stringify(value, replacer, space) {
160161
value = value.valueOf();
161162
}
162163

164+
if (value instanceof LITERALLY_AS_CLASS) {
165+
return value.value;
166+
}
167+
163168
switch (value) {
164169
case null: return 'null';
165170
case true: return 'true';

lib/util.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
class LITERALLY_AS {
2+
constructor(value) {
3+
this.value = value;
4+
}
5+
}
6+
17
module.exports = {
28
isSpaceSeparator(c) {
39
return !!c && /[\u1680\u2000-\u200A\u202F\u205F\u3000]/.test(c);
@@ -44,9 +50,16 @@ module.exports = {
4450
},
4551

4652
isTypeContainer(obj) {
47-
return obj && typeof obj === 'object' && obj.hasOwnProperty('_$_') && obj.hasOwnProperty('_$_value') && Object.keys(obj).length === 2;
53+
return obj && typeof obj === 'object' && obj.hasOwnProperty('_$_') &&
54+
obj.hasOwnProperty('_$_value') && Object.keys(obj).length === 2;
55+
},
56+
57+
LITERALLY_AS: function (value) {
58+
return new LITERALLY_AS(value);
4859
},
4960

61+
LITERALLY_AS_CLASS: LITERALLY_AS,
62+
5063
DELETE: Symbol('DELETE'),
5164
UNDEFINED: Symbol('UNDEFINED')
5265
};

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-z",
3-
"version": "4.0.1",
3+
"version": "4.1.0",
44
"description": "JSON for everyone.",
55
"main": "lib/index.min.js",
66
"types": "lib/index.d.ts",

0 commit comments

Comments
 (0)