Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 4.2.5

* Fixed reviver bug that prevented revival of type containers.

### 4.2.4

* Fixed a reviver bug with quoted property names.
Expand Down
11 changes: 4 additions & 7 deletions lib/options-manager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const big = require('./bignumber-util');
const util = require('./util');
const platform = require('./platform-specifics');
const { ValueSourceWrapper } = require('./util');
const { unwrap } = require('./util');

let globalOptions = {};
let globalParseOptions = {};
Expand Down Expand Up @@ -255,15 +255,12 @@ module.exports = {
let typeName;
let container = false;

/* istanbul ignore next */ // This isn't currently being hit, but I feel better leaving it in for safety.
if (value instanceof ValueSourceWrapper) {
value = value.value;
}
value = unwrap(value);

if (util.isTypeContainer(typeNameOrContainer)) {
container = true;
typeName = typeNameOrContainer._$_;
value = typeNameOrContainer._$_value;
typeName = unwrap(typeNameOrContainer._$_);
value = unwrap(typeNameOrContainer._$_value);
}
else {
typeName = typeNameOrContainer;
Expand Down
8 changes: 4 additions & 4 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1180,10 +1180,10 @@ module.exports = function parse(text, reviver, newOptions) {
parseStates[parseState]();
} while (token.type !== 'eof');

if (typeof reviver === 'function') {
if (reviver) {
const result = internalize({ '': root }, '', reviver);

return result === util.DELETE ? undefined : result;
root = result === util.DELETE ? undefined : result;
}

if (reviveTypedContainers && util.isTypeContainer(root)) {
Expand Down Expand Up @@ -1292,7 +1292,7 @@ module.exports = function parse(text, reviver, newOptions) {
const savedLastPos = lastPos;
const token = newToken(type, value);

if (typeof reviver === 'function' && parseState !== 'beforePropertyName') {
if (reviver && parseState !== 'beforePropertyName') {
token.offset = savedLastPos;
token.source = source.slice(savedLastPos, pos);
token.value = new ValueSourceWrapper(value, token.source);
Expand Down Expand Up @@ -1557,7 +1557,7 @@ module.exports = function parse(text, reviver, newOptions) {
let revived;

try {
const arg = typeof reviver === 'function' ? internalize({ '': current.arg }, '', reviver) : current.arg;
const arg = reviver ? internalize({ '': current.arg }, '', reviver) : current.arg;

revived = optionsMgr.reviveTypeValue(current.name, arg);
}
Expand Down
8 changes: 8 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ module.exports = {
obj.hasOwnProperty('_$_value') && Object.keys(obj).length === 2;
},

unwrap(obj) {
if (obj instanceof ValueSourceWrapper) {
return obj.value;
}

return obj;
},

LITERALLY_AS: function (value) {
return new LITERALLY_AS(value);
},
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-z",
"version": "4.2.4",
"version": "4.2.5",
"description": "JSON for everyone.",
"main": "lib/index.min.js",
"types": "lib/index.d.ts",
Expand Down
12 changes: 9 additions & 3 deletions test/parse.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,15 @@ it('parse(text, reviver)', () => {

it('parse(text, reviver) special cases', () => {
expect(
JSONZ.stringify(JSONZ.parse('{a:12.34d}', (k, v) => typeof v === 'number' ? 88 : v))).to.equal(
'{a:12.34d}',
'should not modify Decimal values'
JSONZ.stringify(JSONZ.parse('[{_$_:"Date",_$_value:"2025-06-01T00:00:00.000Z"}]', (k, v) => v))).to.equal(
"[_Date('2025-06-01T00:00:00.000Z')]",
'should convert type container to Date'
);

expect(
JSONZ.stringify(JSONZ.parse('{_$_:"Date",_$_value:"2025-06-01T00:00:00.000Z"}', (k, v) => v))).to.equal(
"_Date('2025-06-01T00:00:00.000Z')",
'should convert type container at root to Date'
);

expect(
Expand Down