Skip to content

Commit

Permalink
Serialize BigInt as an Object.
Browse files Browse the repository at this point in the history
Not sure I want to keep this, though. It is necessary for IndexedDB, but
it suggests that IndexedDB ought to have its own peculiar flavor of
serialization in the `indexeddb` module if this is necessary.

See #22.
  • Loading branch information
flatheadmill committed Jul 31, 2021
1 parent 2c8b1c6 commit d2f582d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion test/verbatim.t.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require('proof')(21, okay => {
require('proof')(22, okay => {
const Verbatim = require('..')

function cycle (value) {
Expand Down Expand Up @@ -42,4 +42,11 @@ require('proof')(21, okay => {
okay(buffer.toString(), 'a', 'buffer')

okay(cycle({ a: null, b: null }), { a: null, b: null }, 'null should no created references')

// This is getting dubious. I'm not sure I want to support this sort of
// thing since it is nothing I'd ever want in my own software. I'm adding it
// for support of IndexedDB, but if that's the way it's going to be,
// preserving nonsense values like BigInt as Object, then I'll probably
// create a copy of Verbatim that is specific to IndexedDB.
okay(cycle(Object(1n)).valueOf(), 1n, 'bigint object')
})
4 changes: 4 additions & 0 deletions verbatim.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ exports.serialize = function (object) {
array.push(serialize(path.concat(i), value[i], buffers))
}
return array
} else if (value instanceof BigInt) {
return [ '_bigint_object', String(value.valueOf()) ]
} else if (value instanceof Date) {
return [ '_date', value.getTime() ]
} else if (Buffer.isBuffer(value)) {
Expand Down Expand Up @@ -78,6 +80,8 @@ exports.deserialize = function (buffers) {
return function () {} ()
case '_bigint':
return BigInt(value[1])
case '_bigint_object':
return Object(BigInt(value[1]))
}
}
if (value == null) {
Expand Down

0 comments on commit d2f582d

Please sign in to comment.