Skip to content

feat: add support for reviver source - #386

Open
43081j wants to merge 3 commits into
json5:mainfrom
43081j:jg/source-revivals
Open

43081j wants to merge 3 commits into
json5:mainfrom
43081j:jg/source-revivals

Conversation

@43081j

@43081j 43081j commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #361.
Closes #185.

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:

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:

// {foo:303,bar:808}
const slots = [
  // {foo:303,bar:808}
  root,
  Map { foo: 3, bar: 6 }, // slots[3], slots[6]
  0,
  // 303
  303,
  5, // start
  8, // end
  // 808
  808,
  13, // start
  16 // end
];

@jordanbtucker jordanbtucker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good. I checked it against Node's native JSON.parse with a bunch of different revivers, and context matches in every case, including source disappearing after a value is modified. I also didn't see any slowdown when no reviver is passed.

Before merging, can you add a CHANGELOG entry under Unreleased, and document the context argument in the README's reviver section?

Comment thread lib/parse.js Resolved
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
43081j force-pushed the jg/source-revivals branch from c5c0f6b to cb2930a Compare October 19, 2026 09:03
@43081j
43081j requested a review from jordanbtucker October 19, 2026 09:04
@bybraveHQ

Copy link
Copy Markdown
Collaborator

Since this implements the source text access reviver API, it also resolves #185. Worth adding Closes #185 to the description so it gets closed on merge.

@jordanbtucker jordanbtucker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Just a couple doc changes. Happy to push them myself if you’d rather.

Comment thread CHANGELOG.md Resolved
Comment thread CHANGELOG.md Resolved
Comment thread CHANGELOG.md Resolved
Comment thread CHANGELOG.md Resolved
Comment thread README.md Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement proposal-json-parse-with-source API

3 participants