Hacker News new | ask | show | jobs
by bterlson 806 days ago
I have added this note, thanks! In the blog I am mostly trying to show the behavior you get using the (maybe defacto) stdlib with its default configuration, but this is useful data to call out.
1 comments

If you're going to extend Go the courtesy of customizing the parser, oughtn't you do the same for Python (and all the languages)?

To wit, Python's json module has `parse_float` and `parse_int` hooks:

https://docs.python.org/3/library/json.html#encoders-and-dec...

Example:

  >>> json.loads('{"int":12345,"float":123.45}', parse_int=str, parse_float=str)
  {'int': '12345', 'float': '123.45'}
FWIW, when I've cared about interop and controlled the schema, I've specified JSON strings for numbers, along with the range, precision, and representation. This is no worse (nor better) than using RFC 3339 for dates.
I'm just a JS guy trying to understand the world around me and documenting what I find, not trying to be discourteous (or even courteous). I'll add the note about Python, thanks for calling it out. FWIW JS does not have a similar capability so I can't add a note there.
> FWIW JS does not have a similar capability so I can't add a note there

This example on MDN seems to indicate that you can, am I misunderstanding it?

  const bigJSON = '{"gross_gdp": 12345678901234567890}';
  const bigObj = JSON.parse(bigJSON, (key, value, context) => {
    if (key === "gross_gdp") {
      // Ignore the value because it has already lost precision
      return BigInt(context.source);
    }
    return value;
  });
[0]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
> am I misunderstanding it?

The optional `context` parameter is a tc39 proposal. The feature compatibility matrix on the bottom of the MDN page is really confusing because it's showing only when `JSON.parse` was added, not whether the optional `context` parameter is supported.

I've confirmed it's available in:

  - V8 11.4.31
  - Node 20.12.0 (with `--harmony`)
  - Node 21.7.1 (without requiring `--harmony`)
  - Chrome 123.0.6312.107
But not available in:

  Firefox 124.0.2 (ironically) 
  Safari 17.3.1
The original blog post linked to the proposal:

https://tc39.es/proposal-json-parse-with-source/

https://github.com/tc39/proposal-json-parse-with-source

This issue links to the various browser engine tracking bugs:

https://github.com/tc39/proposal-json-parse-with-source/issu...

Which are:

• Chrome/V8: https://bugs.chromium.org/p/v8/issues/detail?id=12955

• Firefox/SpiderMonkey: https://bugzilla.mozilla.org/show_bug.cgi?id=1658310

• Safari/JavaScriptCore: https://bugs.webkit.org/show_bug.cgi?id=248031

The MDN PR documenting the optional `context` parameter was merged just 2 weeks ago:

https://github.com/mdn/content/pull/32697/files

In JS, it's a good idea anyway to use some JSON parsing library instead of JSON.parse.

With Zod, you can use z.bigint() parser. If you take the "parse any JSON" snippet https://zod.dev/?id=json-type and change z.number() to z.bigint(), it should do what you are looking for.

Fair enough! Thank you for the writeup.