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: 3 additions & 1 deletion packages/pglite/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ export const types = {
if (typeof x === 'string') {
return x
} else {
return JSON_stringify(x)
return JSON_stringify(x, (_key: string, value: unknown) =>
typeof value === 'bigint' ? value.toString() : value,
)
}
},
parse: (x: string) => JSON_parse(x),
Expand Down
28 changes: 26 additions & 2 deletions packages/pglite/tests/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,31 @@ describe('serialize', () => {
)
})

it('not blob', () => {
expect(() => types.serializers[17](1)).toThrow()
Comment on lines -147 to -148
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove this?


it('json with bigint value', () => {
// BigInt values nested inside JSON objects must serialize to their string
// representation rather than throwing "Do not know how to serialize a BigInt"
// See https://github.com/electric-sql/pglite/issues/899
expect(types.serializers[114]({ id: 1n })).toEqual('{"id":"1"}')
})

it('json with nested bigint in array', () => {
expect(
types.serializers[114]([{ id: 1n, name: 'a' }, { id: 2n, name: 'b' }]),
).toEqual('[{"id":"1","name":"a"},{"id":"2","name":"b"}]')
})

it('jsonb with bigint value', () => {
expect(types.serializers[3802]({ id: 1n })).toEqual('{"id":"1"}')
})

it('json number values are unchanged', () => {
// Regular numbers must still serialize as numbers, not strings
expect(types.serializers[114]({ id: 1 })).toEqual('{"id":1}')
})

it('json string passthrough unchanged', () => {
// Pre-serialized JSON strings must pass through as-is
expect(types.serializers[114]('{"id":1}')).toEqual('{"id":1}')
})
})