Skip to content

Commit 90e44fa

Browse files
committed
Fix serialization for undefined
When serializing an object that contains `undefined` as a value, the current packer code incorrectly serializes the value as `null` (0xc0). `null` and `undefined` are not the same thing, and should not be serialized into the same binary representation. Strangely, the unpacker actually does support unpacking `undefined` values using the binary representation 0xc1. There was an old pull request (#4) that seems to address this along with a bunch of other seemingly unrelated changes. The GitHub UI seems to think that the PR was merged manually, but the code that fixes this issue never actually made it into the master branch as far as I can tell. Another option is to omit any keys with `undefined` values. This is what `JSON.stringify` does as `JSON` does not support `undefined` natively. When deserialized, the semantics are mostly correct, as accessing a key that doesn't exist will give you `undefined`. This is slightly more efficient, I suppose and maintain the exact same behavior between binarypack and JSON serialization which is a good thing. However, you can observe the difference with `Object.keys`, `Object.hasOwnProperty`, etc.
1 parent c4231da commit 90e44fa

File tree

3 files changed

+3
-3
lines changed

3 files changed

+3
-3
lines changed

dist/binarypack.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ Packer.prototype.pack = function (value) {
283283
this.bufferBuilder.append(0xc2);
284284
}
285285
} else if (type === 'undefined') {
286-
this.bufferBuilder.append(0xc0);
286+
this.bufferBuilder.append(0xc1);
287287
} else if (type === 'object') {
288288
if (value === null) {
289289
this.bufferBuilder.append(0xc0);

0 commit comments

Comments
 (0)