diff options
author | EuAndreh <eu@euandre.org> | 2025-03-31 21:51:40 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2025-03-31 21:51:40 -0300 |
commit | 570ec471d1605318aeefb030cd78682ae442235b (patch) | |
tree | 51e17eabe37c6689f8799b55e6875c3480329a2c /src/content/tils/2021/08 | |
parent | Makefile, mkdeps.sh: Derive index.html and feed.xml from more static "sortdat... (diff) | |
download | euandre.org-570ec471d1605318aeefb030cd78682ae442235b.tar.gz euandre.org-570ec471d1605318aeefb030cd78682ae442235b.tar.xz |
src/content/: Update all files left to asciidoc
Diffstat (limited to 'src/content/tils/2021/08')
-rw-r--r-- | src/content/tils/2021/08/11/js-bigint-reviver.adoc | 81 |
1 files changed, 34 insertions, 47 deletions
diff --git a/src/content/tils/2021/08/11/js-bigint-reviver.adoc b/src/content/tils/2021/08/11/js-bigint-reviver.adoc index d71174d..657248a 100644 --- a/src/content/tils/2021/08/11/js-bigint-reviver.adoc +++ b/src/content/tils/2021/08/11/js-bigint-reviver.adoc @@ -1,39 +1,26 @@ ---- += Encoding and decoding JavaScript BigInt values with reviver -title: Encoding and decoding JavaScript BigInt values with reviver +:reviver-fn: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#using_the_reviver_parameter +:bigint: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt +:json-rfc: https://datatracker.ietf.org/doc/html/rfc8259 -date: 2021-08-11 - -updated_at: 2021-08-13 - -layout: post - -lang: en - -ref: encoding-and-decoding-javascript-bigint-values-with-reviver - ---- - -`JSON.parse()` accepts a second parameter: a [`reviver()` function][reviver]. +`JSON.parse()` accepts a second parameter: a {reviver-fn}[`reviver()` function]. It is a function that can be used to transform the `JSON` values as they're being parsed. -[reviver]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#using_the_reviver_parameter - -As it turns out, when combined with JavaScript's [`BigInt`] type, you can parse -and encode JavaScript `BigInt` numbers via JSON: +As it turns out, when combined with JavaScript's {bigint}[`BigInt`] type, you +can parse and encode JavaScript `BigInt` numbers via JSON: -[`BigInt`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt - -```javascript +[source,javascript] +---- const bigIntReviver = (_, value) => - typeof value === "string" && value.match(/^-?[0-9]+n$/) - ? BigInt(value.slice(0, value.length - 1)) - : value; -``` + typeof value === "string" && value.match(/^-?[0-9]+n$/) + ? BigInt(value.slice(0, value.length - 1)) + : value; +---- -I chose to interpret strings that contains only numbers and an ending `n` suffix -as `BigInt` values, similar to how JavaScript interprets `123` (a number) +I chose to interpret strings that contains only numbers and an ending `n` +suffix as `BigInt` values, similar to how JavaScript interprets `123` (a number) differently from `123n` (a `bigint`); We do those checks before constructing the `BigInt` to avoid throwing needless @@ -42,25 +29,27 @@ become a bottleneck when parsing large JSON values. In order to do the full roundtrip, we now only need the `toJSON()` counterpart: -```javascript +[source,javascript] +---- BigInt.prototype.toJSON = function() { - return this.toString() + "n"; + return this.toString() + "n"; }; -``` +---- With both `bigIntReviver` and `toJSON` defined, we can now successfully parse and encode JavaScript objects with `BigInt` values transparently: -```javascript +[source,javascript] +---- const s = `[ - null, - true, - false, - -1, - 3.14, - "a string", - { "a-number": "-123" }, - { "a-bigint": "-123n" } + null, + true, + false, + -1, + 3.14, + "a string", + { "a-number": "-123" }, + { "a-bigint": "-123n" } ]`; const parsed = JSON.parse(s, bigIntReviver); @@ -71,11 +60,11 @@ console.log(s2); console.log(typeof parsed[6]["a-number"]) console.log(typeof parsed[7]["a-bigint"]) -``` +---- The output of the above is: -``` +.... [ null, true, @@ -89,12 +78,10 @@ The output of the above is: [null,true,false,-1,3.14,"a string",{"a-number":"-123"},{"a-bigint":"-123n"}] string bigint -``` +.... If you're on a web browser, you can probably try copying and pasting the above code on the console right now, as is. -Even though [`JSON`] doesn't include `BigInt` number, encoding and decoding them -as strings is quite trivial on JavaScript. - -[`JSON`]: https://datatracker.ietf.org/doc/html/rfc8259 +Even though {json-rfc}[`JSON`] doesn't include `BigInt` number, encoding and +decoding them as strings is quite trivial on JavaScript. |