Skip to content

Commit ed8a2b3

Browse files
skewb1kdundargoc
authored andcommitted
feat(json): vim.json.encode() sort_keys neovim#35574
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.
1 parent ffc75e0 commit ed8a2b3

File tree

5 files changed

+269
-49
lines changed

5 files changed

+269
-49
lines changed

runtime/doc/lua.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3391,6 +3391,8 @@ vim.json.encode({obj}, {opts}) *vim.json.encode()*
33913391
• indent: (string) (default "") String used for indentation at
33923392
each nesting level. If non-empty enables newlines and a
33933393
space after colons.
3394+
• sort_keys: (boolean) (default false) Sort object keys in
3395+
alphabetical order.
33943396

33953397
Return: ~
33963398
(`string`)

runtime/doc/news.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ LUA
266266
|vim.list.bisect()| for binary search.
267267
• Experimental `vim.pos` and `vim.range` for Position/Range abstraction.
268268
|vim.json.encode()| has an `indent` option for pretty-formatting.
269+
|vim.json.encode()| has an `sort_keys` option.
269270

270271
OPTIONS
271272

runtime/lua/vim/_meta/json.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,7 @@ function vim.json.decode(str, opts) end
5656
--- characters "/" in string values.
5757
--- - indent: (string) (default "") String used for indentation at each nesting level.
5858
--- If non-empty enables newlines and a space after colons.
59+
--- - sort_keys: (boolean) (default false) Sort object
60+
--- keys in alphabetical order.
5961
---@return string
6062
function vim.json.encode(obj, opts) end

0 commit comments

Comments
 (0)