chore: use json-with-bigint for JSON.stringify hook #1312

Merged
Sainan merged 1 commits from jsonstringify into main 2025-03-24 11:31:53 -07:00
3 changed files with 10 additions and 23 deletions

8
package-lock.json generated
View File

@ -14,7 +14,7 @@
"copyfiles": "^2.4.1",
"crc-32": "^1.2.2",
"express": "^5",
"json-with-bigint": "^3.2.1",
"json-with-bigint": "^3.2.2",
"mongoose": "^8.11.0",
"morgan": "^1.10.0",
"typescript": ">=5.5 <5.6.0",
@ -2348,9 +2348,9 @@
"license": "MIT"
},
"node_modules/json-with-bigint": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/json-with-bigint/-/json-with-bigint-3.2.1.tgz",
"integrity": "sha512-0f8RHpU1AwBFwIPmtm71W+cFxzlXdiBmzc3JqydsNDSKSAsr0Lso6KXRbz0h2LRwTIRiHAk/UaD+xaAN5f577w==",
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/json-with-bigint/-/json-with-bigint-3.2.2.tgz",
"integrity": "sha512-zbaZ+MZ2PEcAD0yINpxvlLMKzoC1GPqy5p8/ZgzRJRoB+NCczGrTX9x2ashSvkfYTitQKbV5aYQCJCiHxrzF2w==",
"license": "MIT"
},
"node_modules/json5": {

View File

@ -19,7 +19,7 @@
"copyfiles": "^2.4.1",
"crc-32": "^1.2.2",
"express": "^5",
"json-with-bigint": "^3.2.1",
"json-with-bigint": "^3.2.2",
"mongoose": "^8.11.0",
"morgan": "^1.10.0",
"typescript": ">=5.5 <5.6.0",

View File

@ -9,25 +9,12 @@ import { app } from "./app";
import { config, validateConfig } from "./services/configService";
import { registerLogFileCreationListener } from "@/src/utils/logger";
import mongoose from "mongoose";
import { Json, JSONStringify } from "json-with-bigint";
// Patch JSON.stringify to work flawlessly with Bigints. Yeah, it's not pretty.
// TODO: Might wanna use json-with-bigint if/when possible.
{
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
(BigInt.prototype as any).toJSON = function (): string {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
return "<BIGINT>" + this.toString() + "</BIGINT>";
};
const og_stringify = JSON.stringify;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
JSON.stringify = (obj: any, replacer?: any, space?: string | number): string => {
return og_stringify(obj, replacer as string[], space)
.split(`"<BIGINT>`)
.join(``)
.split(`</BIGINT>"`)
.join(``);
};
}
// Patch JSON.stringify to work flawlessly with Bigints.
JSON.stringify = (obj: Exclude<Json, undefined>, _replacer?: unknown, space?: string | number): string => {
return JSONStringify(obj, space);
};
registerLogFileCreationListener();
validateConfig();