sidorares / json-bigint

JSON.parse/stringify with bigints support
MIT License
790 stars 189 forks source link

Optional schema object as parse arguments to enforce BigInt vs Number #66

Open haoadoreorange opened 2 years ago

haoadoreorange commented 2 years ago

This might solve the limitation o !== JSONbig.parse(JSONbig.stringify(o))

The limitation is there because JS treat BigInt and Number as 2 separate types which cannot be cooerced. The parser decides the appropriate type based on the size of the number in JSON string. Which introduces 2 problems:

There's the option to parse all Number as BigInt but imho this isn't much desirable. Libraries solve problem (2) by iterating the parsed result and enforce the type as you can see here. That PR has an interesting approach which this one is inspired from.

Here's the proposed solution. The parser can take a schema-like object which allow to say whether to use BigInt or Number for specific fields and parse it accordingly. This is a first prototype to show what it might look like.

If this is something viable there's a lot to improve.

haoadoreorange commented 2 years ago

Since I'm needing this, I rewrote the lib in TS with support for this PR here: https://github.com/haoadoresorange/when-json-met-bigint

sidorares commented 2 years ago

hi @haoadoresorange thanks for the PR (and sorry for long silence). Could you add a unit test to cover functionality you are adding?

haoadoreorange commented 2 years ago

hi @haoadoresorange thanks for the PR (and sorry for long silence). Could you add a unit test to cover functionality you are adding?

Hi,

Unfortunately at the moment I have no interest in continuing this PR ): as I said I forked the project and implemented this PR here https://github.com/haoadoresorange/when-json-met-bigint

Also I fixed several bugs to align with the newest spec behaviour (: you can give it a look if you like.

haoadoreorange commented 2 years ago

Hi @sidorares, me again 😄

I talked to @MatthewKhouzam today (I believe he talked to you a couple days ago). So apparently merging my forked back to the main repo will benefit greatly everyone, and I'd be gladly to do so !

However the fork as of now is very much diversed from this repo, the original core is similar though. So now I'm not sure how to move forward with the merge and I need your help to make it work (: Could I reach you so we can talk about this a little bit more in detail ?

EDIT: typo