Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Post-process parsed JSON to turn any necessary number as BigInt
The BigInt library has no way to know whether a type should be a normal number or a bigint when parsing, because there's no reflection available to JS code at runtime. Any type information from TypeScript is gone, and therefore, the library makes the choice of using bigint only when necessary. This is in practice annoying, as it causes TypeScript to behave as if some type was a BigInt, whereas they are in reality parsed as _number_, causing runtime errors upon the first manipulation. We have explored three possible solutions, and this commit implements the second one (while the third one is being worked but is more involved). 1/ Return types as `number | bigint`, since this is what they are in the end. This pushes the concern down to the consumer which will have to deal with it in a quite ugly way. 2/ Perform a second pass on the parsed data, to turn into bigint numbers which _should be_ (according to their type definition), bigint, but have been serialized as numbers. Not fully ideal because it requires to re-process the entire JSON object, and, it makes strong assumption on the shape of the JSON object to identify which fields should be turned into bigint and which one should be preserved. In the long run, this isn't very maintainable as it'll break as soon as we add a new field with a bigint type and forget to add it to the post-process checks. 3/ Fork the bigint library, such that a JSON-schema can be given to the parser, which it can traverse while parsing a string, and consult to figure out _which_ type should a number be deserialized into. This is really non-trivial since JSON-schema supports compound operators such as `anyOf`, `oneOf` and `allOf`, requiring the parser to maintain a tree of possibilities which it attempts to narrow down as it traverse the tree.
- Loading branch information