question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSON.stringify() doesn't know how to serialize a BigInt

See original GitHub issue

Not certain how to handle this situation:

    TypeError: Do not know how to serialize a BigInt
        at JSON.stringify (<anonymous>)

      39 |     toObject() {
    > 40 |         return JSON.parse(JSON.stringify(this));
         |                                ^
      41 |     }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:9

github_iconTop GitHub Comments

175reactions
cliffhallcommented, Aug 22, 2019

@jakobkummerow Ok, makes sense. For anyone else stumbling upon this, the verified TL;DR fix for the above problem is:

    toObject() {
        return JSON.parse(JSON.stringify(this, (key, value) =>
            typeof value === 'bigint'
                ? value.toString()
                : value // return everything else unchanged
        ));
    }
56reactions
ADTCcommented, Nov 9, 2022

Wouldn’t it be easier to just monkey patch (hijack) BigInt? That’s the MDN recommendation.

BigInt.prototype.toJSON = function() { return this.toString() }

If you do it this way, you don’t have to add an anonymous function into every call of JSON.stringify. It applies globally to all such calls.

Read more comments on GitHub >

github_iconTop Results From Across the Web

bigint - # - json
Serialization and Deserialization ... It should be noted that how you choose to serialize your BigInts affects how you deserialize your BigInts.
Read more >
JSON stringify and PostgreSQL bigint compliance
The nature of the library permits not to worry about type ambiguity and de-serialization, as everything that's serialized goes into the server, ...
Read more >
"TypeError: Do not know how to serialize a BigInt" - General
2nd thing to know, is a BigInt cannot be directly serialised to JSON ... to serialise (likely JSON.stringify) the data before being written...
Read more >
24 Typeerror: Do Not Know How To Serialize A Bigint 12 ...
Below is the best information and knowledge about typeerror: do not know how to serialize a bigint compiled and compiled by the BMR...
Read more >
JSON.stringify() - JavaScript - MDN Web Docs
The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found