Languages

JavaScript - Uncaught TypeError: Do not know how to serialize a BigInt

7 points
Asked by:
Zayaan-Rasmussen
543

Any idea, why JSON.stringify() throws:

when I try to use:

1 answer
5 points
Answered by:
Root-ssh
178180

JSON.stringify() doesn't support BitInt type.

You can solve the problem by:

  1. own non-standard JSON.stringify() implementaion,
  2. sending Number or String type instead of BigInt type,
  3. 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:

e.g. 2:

Practical examples

e.g. 1:

e.g. 2:

 

Solution 3

As example we can use for BigInt type objects some wrapping object.

e.g.

 

0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join