feat: toJSON method support for custom serialization (#237)

* feat: add toJSON method support for custom serialization

* fix: prevent infinite recursion

* test: remove redundant toJSON test cases

* docs: add custom serialization details for toJSON method

* test: fix type issues

---------

Co-authored-by: Johann Schopplich <mail@johannschopplich.com>
This commit is contained in:
Viliam Kopecký
2025-12-04 14:08:56 +01:00
committed by GitHub
parent 7ed9701028
commit a4538b48e7
4 changed files with 189 additions and 0 deletions

View File

@@ -54,6 +54,7 @@ Non-JSON-serializable values are normalized before encoding:
| Input | Output |
|-------|--------|
| `Object` with `toJSON()` method | Result of calling `toJSON()`, recursively normalized |
| Finite number | Canonical decimal (no exponent, no leading/trailing zeros: `1e6` → `1000000`, `-0` → `0`) |
| `NaN`, `Infinity`, `-Infinity` | `null` |
| `BigInt` (within safe range) | Number |