TypeError: Do not know how to serialize a BigInt - JSON encoding too limited
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 63.2k
- Forks
- 10.3k
- Avg merge
- 11d 20h
- Merged PRs (30d)
- 2
Description
Describe the bug
Cannot send BigInt values. Will result in the following error:
TypeError: Do not know how to serialize a BigInt
at JSON.stringify (<anonymous>) at Encoder.encodeAsString (C:\Users\user\code\myproj\node_modules\socket.io-parser\dist\index.js:69:25) at Encoder.encode (C:\Users\user\code\myproj\node_modules\socket.io-parser\dist\index.js:45:22) at Manager._packet (C:\Users\user\code\myproj\node_modules\socket.io-client\build\manager.js:257:45) at Socket.packet (C:\Users\user\code\myproj\node_modules\socket.io-client\build\socket.js:151:17) at Socket.emit (C:\Users\user\code\myproj\node_modules\socket.io-client\build\socket.js:135:18) at sendNow (C:\Users\user\code\myproj\src\client\Client.js:200:20) at flush (C:\Users\user\code\myproj\src\client\SendQueue.js:45:21) at Timeout._onTimeout (C:\Users\user\code\myproj\src\client\SendQueue.js:36:42)
To Reproduce
Socket.IO server version: any (tested with 2.x and also with latest 4.x)
Server
import { Server } from "socket.io";
const io = new Server(3000, {});
io.on("connection", (socket) => {
console.log(`connect ${socket.id}`);
socket.on("disconnect", () => {
console.log(`disconnect ${socket.id}`);
});
});
Socket.IO client version: same as server
Client
import { io } from "socket.io-client";
const socket = io("ws://localhost:3000/", {});
socket.on("connect", () => {
console.log(`connect ${socket.id}`);
socket.emit('test', 1n); // send BigInt here
});
socket.on("disconnect", () => {
console.log("disconnect");
});
Expected behavior
BigInt (and other es6+ data types) serialization should be supported.
Related
This also used to cause #3353 (probably only in older versions, where error reporting was less verbose).
Solution Suggestion
The shortcomings of JSON have been well established. Good thing is that if socket.io moves from JSON.serialize to serialize-javascript (as a drop-in replacement), BigInt and a lot of other new stuff would just work out of the box.
Please consider moving away from JSON.serialize to a more modern serialization approach?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The failing entry point is socket.io-parser's Encoder.encodeAsString, shown in the stack trace; start by reproducing the client emit of 1n and inspecting the JSON-based encoding path. Compare the current behavior with the proposed serialization approach, then confirm that emitting BigInt no longer throws and that existing packet encoding behavior remains intact.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- api, networking
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100