msgpackr 1.9.4 → 1.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/test.js CHANGED
@@ -1635,8 +1635,13 @@
1635
1635
  if (Array.isArray(value)) {
1636
1636
  packArray(value);
1637
1637
  } else {
1638
- if (value.toJSON) // use this as an alternate mechanism for expressing how to serialize
1639
- return pack(value.toJSON());
1638
+ // use this as an alternate mechanism for expressing how to serialize
1639
+ if (value.toJSON) {
1640
+ const json = value.toJSON();
1641
+ // if for some reason value.toJSON returns itself it'll loop forever
1642
+ if (json !== value)
1643
+ return pack(json)
1644
+ }
1640
1645
 
1641
1646
  // if there is a writeFunction, use it, otherwise just encode as undefined
1642
1647
  if (type === 'function')
@@ -4098,6 +4103,17 @@
4098
4103
  assert.deepEqual(values, [[1,0,1], [2,1,2], [3,2,3], [4,3,4]]);
4099
4104
  });
4100
4105
 
4106
+ test('pack toJSON returning this', () => {
4107
+ class Serializable {
4108
+ someData = [1, 2, 3, 4]
4109
+ toJSON() {
4110
+ return this
4111
+ }
4112
+ }
4113
+ const serialized = pack(new Serializable);
4114
+ const deserialized = unpack(serialized);
4115
+ assert.deepStrictEqual(deserialized, { someData: [1, 2, 3, 4] });
4116
+ });
4101
4117
  });
4102
4118
  suite('msgpackr performance tests', function(){
4103
4119
  test('performance JSON.parse', function() {