fix: handle BigInt values in JSON type serializer#942
Open
claygeo wants to merge 2 commits intoelectric-sql:mainfrom
Open
fix: handle BigInt values in JSON type serializer#942claygeo wants to merge 2 commits intoelectric-sql:mainfrom
claygeo wants to merge 2 commits intoelectric-sql:mainfrom
Conversation
When arrays or objects containing BigInt values are passed as JSON parameters (e.g., to json_to_recordset), the JSON serializer calls native JSON.stringify which throws "Do not know how to serialize a BigInt". The fix adds a replacer function that converts BigInt values to strings during JSON serialization. This matches the behavior of the standalone bigint type handler (which uses .toString()) and is consistent with the common BigInt.prototype.toJSON workaround. This particularly affects pglite-sync's json initial insert mode, which uses json_to_recordset for bulk inserts and BigInt for LSN tracking. Closes electric-sql#899
Collaborator
|
@claygeo Thank you for this! Could you please add the tests in your 'Test Plan' to |
Covers all four cases from the PR test plan: - BigInt value nested in a JSON object serializes to string (no throw) - BigInt value nested in a JSON array of objects serializes correctly - JSONB serializer handles BigInt the same as JSON - Regular number values are preserved as numbers (regression guard) - Pre-serialized string passthrough is unchanged
Author
|
Added the tests to
Let me know if you'd like any changes! |
tdrz
reviewed
Mar 30, 2026
Comment on lines
-147
to
-148
| it('not blob', () => { | ||
| expect(() => types.serializers[17](1)).toThrow() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When arrays or objects containing BigInt values are passed as JSON/JSONB parameters, the JSON serializer throws:
Reproduction:
The standalone
biginttype handler (OID 20) correctly serializes BigInt via.toString(), but when BigInt values are nested inside JSON/JSONB parameters (OID 114/3802), the JSON serializer attypes.ts:115calls nativeJSON.stringifywhich has no BigInt support.This particularly affects
pglite-sync's JSON initial insert mode, which usesjson_to_recordsetfor bulk inserts and BigInt for LSN tracking.Solution
Add a
replacerfunction toJSON.stringifyin the JSON type serializer that converts BigInt values to their string representation. This matches the behavior of the standalone bigint type handler and is consistent with the commonBigInt.prototype.toJSONworkaround.Changes
packages/pglite/src/types.ts— Added BigInt replacer to JSON type serializerTest Plan
pg.query('SELECT $1::json', [{ id: 1n }])— no longer throwspg.query('SELECT $1::json', [{ id: 1 }])— existing behavior preserved (numbers unchanged)pg.query('SELECT $1::json', ['{"id": 1}'])— string passthrough unchangedpg.query('SELECT $1::int8', [1n])— standalone bigint handler still worksCloses #899