[15.x] Fix: Serialization does not handle non-enumerable properties in Node versions < 5.x (#677) (#680)

Backports the following commits to 15.x:
 - Fix: Serialization does not handle non-enumerable properties in Node versions < 5.x  (#677)
This commit is contained in:
Spencer
2018-07-03 12:09:47 -07:00
committed by GitHub
parent 61f793c1ae
commit 9147d75b9f
2 changed files with 23 additions and 1 deletions

View File

@ -20,7 +20,11 @@ Json.prototype.serialize = function (val, replacer, spaces) {
return val;
case 'object':
if (val) {
return JSON.stringify(val, replacer, spaces);
if (replacer || spaces) {
return JSON.stringify(val, replacer, spaces);
} else {
return JSON.stringify(val);
}
}
/* falls through */
default:

View File

@ -39,6 +39,24 @@ describe('JSON serializer', function () {
ser.serialize(thing);
}).to.throwError();
});
it('utilizes replacer or spaces if passed', function () {
sinon.spy(JSON, 'stringify');
var ser = makeSerializer();
var thing = { name: 'thing' };
ser.serialize(thing, null, 2);
expect(JSON.stringify.withArgs(thing, null, 2).calledOnce).to.be(true);
JSON.stringify.restore();
});
it('should call JSON.stringify with value only', function () {
sinon.spy(JSON, 'stringify');
var ser = makeSerializer();
var thing = { name: 'thing' };
ser.serialize(thing);
expect(JSON.stringify.withArgs(thing).calledOnce).to.be(true);
JSON.stringify.restore();
});
});
describe('#deserialize', function () {