Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to parse bigints when using Response.json() function? #34

Open
pspraveenkr opened this issue Jul 28, 2022 · 5 comments
Open

How to parse bigints when using Response.json() function? #34

pspraveenkr opened this issue Jul 28, 2022 · 5 comments

Comments

@pspraveenkr
Copy link

In many cases, JSON parsing happens indirectly when an application calls Response.json() function after fetching JSON payload from a service using the Fetch API.

Would it be within the scope of this proposal to enhance Response.json() to add support for reviver option?
Or, is the recommendation to use Response.text() and then call JSON.parse directly with a reviver function?

Thank you for advancing this proposal! We at Esri are tracking this proposal as part of adding support for big integers to our ArcGIS Online Cloud platform. Not being able to serialize and de-serialize is a blocker at the moment.

@pspraveenkr pspraveenkr changed the title Question: how to parse bigints when using Response.json() function? How to parse bigints when using Response.json() function? Jul 28, 2022
@ljharb
Copy link
Member

ljharb commented Jul 28, 2022

Response isn’t a thing in the language - it’d be the job of, in this case, HTML to add support for it.

@pspraveenkr
Copy link
Author

I will open an issue at https://github.com/whatwg/fetch which looks like the right spot for this question.

Thanks!

@WebReflection
Copy link

WebReflection commented Oct 9, 2024

I just tried:

JSON.parse(JSON.stringify(JSON.rawJSON(12345678901234567890n)), (k, v) => { console.log({ k, v }); return v; })

and there's no way I can get the original 12345678901234567890 value in there ... this is not even a reviver functionality, this is about creating JSON no JS runtime can understand and I start wondering what was the original idea around it ... I understand we can get to not lose big numbers when serializing, but it's not clear at all how are we supposed to also consume such big numbers (and basically nothing else that's interesting) once such serialization happens ... is this an API for foreign PLs and not actually or JS ? 🤔

edit never mind, I've just realized the source is different from the returned value ... how bizarre, I should've seen that coming in the reviver.

@bakkot
Copy link

bakkot commented Oct 9, 2024

@WebReflection

JSON.parse(
  JSON.stringify(JSON.rawJSON(12345678901234567890n)),
  (k, v, c) => { if (typeof v === 'number') return BigInt(c.source); return v; }
)

@WebReflection
Copy link

@bakkot ... not quite:

const reviver = (_, value, context) => (
  context && typeof value === 'number' && String(value) !== context.source ?
    BigInt(context.source) : value
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants