Skip to content
Open
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: 2 additions & 2 deletions cjs/deserialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ const deserializer = ($, _) => {
case 'BigInt':
return as(Object(BigInt(value)), index);
case 'ArrayBuffer':
return as(new Uint8Array(value).buffer, value);
return as(new Uint8Array(value).buffer, index);
case 'DataView': {
const { buffer } = new Uint8Array(value);
return as(new DataView(buffer), value);
return as(new DataView(buffer), index);
}
case '-0': return -0;
}
Expand Down
4 changes: 2 additions & 2 deletions esm/deserialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ const deserializer = ($, _) => {
case 'BigInt':
return as(Object(BigInt(value)), index);
case 'ArrayBuffer':
return as(new Uint8Array(value).buffer, value);
return as(new Uint8Array(value).buffer, index);
case 'DataView': {
const { buffer } = new Uint8Array(value);
return as(new DataView(buffer), value);
return as(new DataView(buffer), index);
}
case '-0': return -0;
}
Expand Down
2 changes: 1 addition & 1 deletion structured-json.js

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

19 changes: 19 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ const assert = (expected, result, message = '') => {
}
};

{
const buffer = new Uint8Array([10, 20, 30]).buffer;
for (const value of [buffer, new DataView(buffer)]) {
const original = {
first: value,
second: value,
map: new Map([[value, value]])
};
for (const result of [deserialize(serialize(original)), parse(stringify(original))]) {
assert(result.first === value, false, 'clones the original value');
assert(result.first, result.second, 'preserves repeated buffer or view references');
assert(result.map.get(result.first), result.first, 'preserves Map key and value references');
const bytes = result.first instanceof DataView ?
new Uint8Array(result.first.buffer) : new Uint8Array(result.first);
assert([...bytes].join(','), '10,20,30');
}
}
}

// @see https://github.com/ungap/structured-clone/issues/4
const withUndefined = stringify({foo: 'test', bar: undefined, foobar: null});
assert(withUndefined, '[[2,[[1,2],[3,4],[5,6]]],[0,"foo"],[0,"test"],[0,"bar"],[-1],[0,"foobar"],[0,null]]');
Expand Down