Skip to content

Added reviver support for "JSON.parse source text access" proposal. - #361

Closed
kshetline wants to merge 4 commits into
json5:mainfrom
kshetline:main
Closed

kshetline wants to merge 4 commits into
json5:mainfrom
kshetline:main

Conversation

@kshetline

@kshetline kshetline commented May 25, 2025 •

Copy link
Copy Markdown

As I added to the README.md file:

The reviver function supports a third argument, after key and value, called context, which is provided when value has been parsed as a primitive (string, number, boolean, or null). The source text for the primitive is provided bycontext.source allowing, for example, processing numbers with greater precision than native JavaScript numbers can handle.

(This is in accordance with a proposal, documented here, which is not yet part of the official ECMAScript standard, but is nevertheless widely supported at present.)

@kshetline

kshetline commented May 25, 2025 •

Copy link
Copy Markdown
Author

By the way, if you're interested, I'd love to update your testing suite to mocha/chai. Besides all of the security warnings your old version of tap in producing, I went nuts tracking down the single original unit test that failed after I first made most of my code changes. The JavaScript source line numbers were way off, and breakpoint positions were wildly out of place. That made this one bug really hard to track down.

I've already done the mocha/chai work in my own JSON5 fork, which I hadn't touched for nearly five years until this last month.

My fork is a crazy excessive add-on to JSON5, which I'm pretty sure has no chance of catching on like JSON5, but it's my strange idea of fun anyway, and I like having it for myself.

@aseemk

aseemk commented May 27, 2025

Copy link
Copy Markdown
Member

Very cool! Thank you for this contribution! =)

@jordanbtucker: would love your 👀; curious if you have any concerns or if this LGTY too.

Ditto on both counts for the offer to update our tests!

@kshetline

Copy link
Copy Markdown
Author

I've got the test updates now on another branch. I'll wait until the disposition of this PR has been determined before I add another to the pending pile. I think you'll like the results, however. Clean audit with 0 warnings.

image

@kshetline

Copy link
Copy Markdown
Author

By the way, I just got a PR for the highlight.js project accepted so that it now will recognize and highlight JSON5.

@jordanbtucker jordanbtucker added this to the v3.0.0 milestone Oct 14, 2026
@jordanbtucker

Copy link
Copy Markdown
Member

@kshetline Thanks for your work on this. With the source text access at Stage 4, I'm happy to include this in JSON5. If you're still interested in merging this, it will need a bit of work since we just merged a bit of an overhaul to main. Let me know if you're interested in porting it.

@kshetline

Copy link
Copy Markdown
Author

I'm glad to see that things have picked up here again. It'll take me a little while to remember where I left off, but yes, I can try to rework this PR to fit properly with the current state of the project.

@jordanbtucker

Copy link
Copy Markdown
Member

Awesome! Thanks for your support. Would you mind reviewing #386. It looks like this feature has already been ported onto main.

@kshetline

Copy link
Copy Markdown
Author

Awesome! Thanks for your support. Would you mind reviewing #386. It looks like this feature has already been ported onto main.

Looks good to me.

@jordanbtucker

Copy link
Copy Markdown
Member

Superseded by #386

43081j added a commit to 43081j/json5 that referenced this pull request Oct 19, 2026
Closes json5#361.

The parse loop has changed quite a lot since that PR was opened so its
pretty much a full rework to add this feature either way.

This adds support for `context.source` when using a reviver, just the
same as `JSON.parse`.

Note that this may look a bit funky but that's for performance reasons.
Instead of allocating an AST-like set of objects to track position info,
we hold a collection of slots.

The slots collection looks like this:

```ts
type PrimitiveSlot = [value: unknown, start: number, end: number];
type ChildMeta = Array<number> | Map<string, number>
type ContainerSlot = [value: unknown, childMeta: ChildMeta, end: 0];
```

This results in something like this:

```ts
// {foo:303,bar:808}
const slots = [
  // {foo:303,bar:808}
  root,
  Map { foo: 3, bar: 6 },
  0,
  // 303
  303,
  5,
  8,
  // 808
  808,
  13,
  16
];
```
43081j added a commit to 43081j/json5 that referenced this pull request Oct 19, 2026
Closes json5#361.

The parse loop has changed quite a lot since that PR was opened so its
pretty much a full rework to add this feature either way.

This adds support for `context.source` when using a reviver, just the
same as `JSON.parse`.

Note that this may look a bit funky but that's for performance reasons.
Instead of allocating an AST-like set of objects to track position info,
we hold a collection of slots.

The slots collection looks like this:

```ts
type PrimitiveSlot = [value: unknown, start: number, end: number];
type ChildMeta = Array<number> | Map<string, number>
type ContainerSlot = [value: unknown, childMeta: ChildMeta, end: 0];
```

This results in something like this:

```ts
// {foo:303,bar:808}
const slots = [
  // {foo:303,bar:808}
  root,
  Map { foo: 3, bar: 6 },
  0,
  // 303
  303,
  5,
  8,
  // 808
  808,
  13,
  16
];
```
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

Successfully merging this pull request may close these issues.

3 participants