JohnWeisz / TypedJSON

Typed JSON parsing and serializing for TypeScript that preserves type information.
MIT License
603 stars 64 forks source link

Add support to use external JSON parser #154

Closed PeterKnego closed 3 years ago

PeterKnego commented 3 years ago

TypedJSON by default uses JSON parser provided by browser. The problem with the default parser is that it mangles BigInt values. So there is no option to write a TypedJSON deserializer that would properly handle bigint values, as they are already rounded to Javascript numbers.

Solution: provide a way to define external parser during the TypedJson setup. There are parsers that properly handle bigint numbers in JSON, e.g. json-bigint.

Note: there is already a way to serialize objects with bigint fields: TypedJSON prvides toPlainJson function that produces plain JSON objects, that can then be serialized with parsers that properly handle bigint numbers in JSON, e.g. json-bigint.

PeterKnego commented 3 years ago

I was able to resolve proper BigInt parsing by first using json-bigint parser and then run returned object through TypedJSON.parse().

let jsonObj = JSONbig.parse(jsonString);
let value: RES | undefined = TypedJSON.parse(jsonObj, responseType)