Skip to content

Conversation

@skewb1k
Copy link
Contributor

@skewb1k skewb1k commented Aug 31, 2025

Problem

There is no way to ensure a stable key order when encoding a JSON string, which can be useful for comparisons and producing cleaner diffs.

Solution

Introduce a sort_keys option for vim.json.encode(),which is disabled by default. When enabled, object keys are sorted in alphabetical order.

Adapts PR to upstream: openresty/lua-cjson#115.

Uses the most performant approach to sort keys I could find. A small benchmark with comparisons is available here: https://github.com/skewb1k/lua-c-sort-keys-benchmark

Updates #34776 I guess? @echasnovski

@skewb1k skewb1k force-pushed the feat/json-sort-keys branch from 78ae531 to 70e61ec Compare August 31, 2025 19:44
Problem: There is no way to ensure a stable key order when encoding a JSON string,
which can be useful for comparisons and producing cleaner diffs.

Solution: Introduce a `sort_keys` option for `vim.json.encode()`,which
is disabled by default. When enabled, object keys are sorted in
alphabetical order.

Adapts PR to upstream: openresty/lua-cjson#115.
@skewb1k skewb1k force-pushed the feat/json-sort-keys branch from 70e61ec to bdcbc3b Compare September 9, 2025 08:42
@justinmk
Copy link
Member

which can be useful for comparisons and producing cleaner diffs.

also for "stable" representation: https://github.com/ljharb/json-stable-stringify

@skewb1k
Copy link
Contributor Author

skewb1k commented Sep 12, 2025

@justinmk that's partly what I meant by "comparisons". Should I update the commit message?

@justinmk justinmk merged commit a897cc1 into neovim:master Sep 14, 2025
33 checks passed
@skewb1k skewb1k deleted the feat/json-sort-keys branch September 24, 2025 23:02
dundargoc pushed a commit to dundargoc/neovim that referenced this pull request Sep 27, 2025
Problem: There is no way to ensure a stable key order when encoding a JSON string,
which can be useful for comparisons and producing cleaner diffs.

Solution: Introduce a `sort_keys` option for `vim.json.encode()`,which
is disabled by default. When enabled, object keys are sorted in
alphabetical order.
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.

2 participants