msgpackr 1.9.3 → 1.9.5-debug.1

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')
@@ -1681,7 +1686,7 @@
1681
1686
  }
1682
1687
  };
1683
1688
 
1684
- const writePlainObject = this.variableMapSize ? (object) => {
1689
+ const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber) ? (object) => {
1685
1690
  // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
1686
1691
  let keys = Object.keys(object);
1687
1692
  let length = keys.length;
@@ -1697,9 +1702,19 @@
1697
1702
  position += 4;
1698
1703
  }
1699
1704
  let key;
1700
- for (let i = 0; i < length; i++) {
1701
- pack(key = keys[i]);
1702
- pack(object[key]);
1705
+ if (this.coercibleKeyAsNumber) {
1706
+ for (let i = 0; i < length; i++) {
1707
+ key = keys[i];
1708
+ let num = Number(key);
1709
+ pack(isNaN(num) ? key : num);
1710
+ pack(object[key]);
1711
+ }
1712
+
1713
+ } else {
1714
+ for (let i = 0; i < length; i++) {
1715
+ pack(key = keys[i]);
1716
+ pack(object[key]);
1717
+ }
1703
1718
  }
1704
1719
  } :
1705
1720
  (object, safePrototype) => {
@@ -2848,11 +2863,16 @@
2848
2863
  objectLiteralProperties.push('__proto__:this');
2849
2864
  }
2850
2865
  let toObject = (new Function(...args, 'return function(s){return{' + objectLiteralProperties.join(',') + '}}')).apply(null, properties.map(prop => prop.get));
2851
- Object.defineProperty(prototype, 'toJSON', {
2852
- value(omitUnderscoredProperties) {
2853
- return toObject.call(this, this[sourceSymbol]);
2854
- }
2855
- });
2866
+ try {
2867
+ Object.defineProperty(prototype, 'toJSON', {
2868
+ value(omitUnderscoredProperties) {
2869
+ return toObject.call(this, this[sourceSymbol]);
2870
+ }
2871
+ });
2872
+ } catch(error) {
2873
+ error.message += ' setting properties ' + JSON.stringify(properties);
2874
+ throw error;
2875
+ }
2856
2876
  } else {
2857
2877
  Object.defineProperty(prototype, 'toJSON', {
2858
2878
  value(omitUnderscoredProperties) {
@@ -3213,6 +3233,13 @@
3213
3233
  assert.deepEqual(unpacked.map, {});
3214
3234
  assert.deepEqual(unpacked.set, {});
3215
3235
  });
3236
+ test('pack/unpack numeric coercible keys', function () {
3237
+ var data = { a: 1, 2: 'test', '-3.45': 'test2'};
3238
+ let packr = new Packr({variableMapSize: true, coercibleKeyAsNumber: true, useRecords: false});
3239
+ var serialized = packr.pack(data);
3240
+ var deserialized = packr.unpack(serialized);
3241
+ assert.deepEqual(deserialized, data);
3242
+ });
3216
3243
  test('pack/unpack empty data with bundled strings', function () {
3217
3244
  var data = {};
3218
3245
  let packr = new Packr({bundleStrings: true});
@@ -4081,6 +4108,17 @@
4081
4108
  assert.deepEqual(values, [[1,0,1], [2,1,2], [3,2,3], [4,3,4]]);
4082
4109
  });
4083
4110
 
4111
+ test('pack toJSON returning this', () => {
4112
+ class Serializable {
4113
+ someData = [1, 2, 3, 4]
4114
+ toJSON() {
4115
+ return this
4116
+ }
4117
+ }
4118
+ const serialized = pack(new Serializable);
4119
+ const deserialized = unpack(serialized);
4120
+ assert.deepStrictEqual(deserialized, { someData: [1, 2, 3, 4] });
4121
+ });
4084
4122
  });
4085
4123
  suite('msgpackr performance tests', function(){
4086
4124
  test('performance JSON.parse', function() {