EN
JavaScript - Uncaught TypeError: Do not know how to serialize a BigInt
1 answers
7 points
Any idea, why JSON.stringify()
throws:
xxxxxxxxxx
1
Uncaught TypeError: Do not know how to serialize a BigInt
when I try to use:
xxxxxxxxxx
1
const object = {
2
key: 'code',
3
value: 123n
4
};
5
6
const json = JSON.stringify(object);
7
8
console.log(json);
1 answer
5 points
JSON.stringify()
doesn't support BitInt type.
You can solve the problem by:
- own non-standard
JSON.stringify()
implementaion, - sending
Number
orString
type instead ofBigInt
type, - using some wrapping source code.
Solution 1
That approach is too complicated and breaks JSON standard rules, so I will focus on other solutions.
Solution 2
As example we can use String
type to store BigInt
type, knowing the value can be very big.
The main disadvantage of this solution is necessity to keep information about what properties have BigInt type.
Quick solutions
e.g. 1:
xxxxxxxxxx
1
BigInt.prototype.toJSON = function() { return this.toString() };
e.g. 2:
xxxxxxxxxx
1
const json = JSON.stringify(object , (key, value) => {
2
return typeof value === 'bigint' ? value.toString() : value;
3
});
Practical examples
e.g. 1:
xxxxxxxxxx
1
BigInt.prototype.toJSON = function() { // <------------
2
return this.toString(); // <--- SOLUTION
3
}; // <------------
4
5
const inputObject = {
6
key: 'code',
7
value: 123n
8
};
9
10
const customJson = JSON.stringify(inputObject);
11
const outputObject = JSON.parse(customJson);
12
13
console.log(`JSON: ${customJson}\n\n`);
14
console.log(`TYPE: ${typeof outputObject.key} VALUE: ${outputObject.key}`);
15
console.log(`TYPE: ${typeof outputObject.value} VALUE: ${outputObject.value}`);
e.g. 2:
xxxxxxxxxx
1
const inputObject = {
2
key: 'code',
3
value: 123n
4
};
5
6
const customJson = JSON.stringify(inputObject , (key, value) => { // <------------
7
return typeof value === 'bigint' ? value.toString() : value; // <--- SOLUTION
8
}); // <------------
9
10
const outputObject = JSON.parse(customJson);
11
12
console.log(`JSON: ${customJson}\n\n`);
13
console.log(`TYPE: ${typeof outputObject.key} VALUE: ${outputObject.key}`);
14
console.log(`TYPE: ${typeof outputObject.value} VALUE: ${outputObject.value}`); // we need to do conversion to BigInt manually, e.g. BigInt(outputObject.value)
Solution 3
As example we can use for BigInt
type objects some wrapping object.
e.g.
xxxxxxxxxx
1
const renderJson = (object) => {
2
return JSON.stringify(object, (key, value) => {
3
switch (typeof value) {
4
case 'bigint':
5
return { // warpper
6
$T$: 'bigint', // type // maybe it is good to use some more complicated name instead of $T$
7
$V$: value.toString() // value // maybe it is good to use some more complicated name instead of $V$
8
};
9
// Put more cases here ...
10
default:
11
return value;
12
}
13
});
14
};
15
16
const pareseJson = (json) => {
17
return JSON.parse(json, (key, value) => {
18
if (typeof value === 'object') {
19
switch (value?.$T$) {
20
case 'bigint': // warpper
21
return BigInt(value.$V$);
22
// Put more cases here ...
23
default:
24
return value;
25
}
26
} else {
27
return value;
28
}
29
});
30
};
31
32
33
// Usage example:
34
35
const inputObject = {
36
key: 'code',
37
value: 123n
38
};
39
40
const customJson = renderJson(inputObject);
41
const outputObject = pareseJson(customJson);
42
43
console.log(`JSON: ${customJson}\n\n`);
44
console.log(`TYPE: ${typeof outputObject.key} VALUE: ${outputObject.key}`);
45
console.log(`TYPE: ${typeof outputObject.value} VALUE: ${outputObject.value}`);
0 commentsShow commentsAdd comment