Skip to content

Commit 70e61ec

Browse files
committed
feat(json): sort_keys option to vim.json.encode()
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.
1 parent 1b3abfa commit 70e61ec

File tree

6 files changed

+271
-58
lines changed

6 files changed

+271
-58
lines changed

runtime/doc/lua.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3375,6 +3375,8 @@ vim.json.encode({obj}, {opts}) *vim.json.encode()*
33753375
{opts} (`table<string,any>?`) Options table with keys:
33763376
• escape_slash: (boolean) (default false) Escape slash
33773377
characters "/" in string values.
3378+
• sort_keys: (boolean) (default false) Sort object keys in
3379+
alphabetical order.
33783380

33793381
Return: ~
33803382
(`string`)

runtime/doc/news.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ LUA
256256
|vim.list.unique()| to deduplicate lists.
257257
|vim.list.bisect()| for binary search.
258258
• Experimental `vim.pos` and `vim.range` for Position/Range abstraction.
259+
|vim.json.encode()| has an `sort_keys` option.
259260

260261
OPTIONS
261262

runtime/lua/vim/_meta/json.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,7 @@ function vim.json.decode(str, opts) end
3838
---@param opts? table<string,any> Options table with keys:
3939
--- - escape_slash: (boolean) (default false) Escape slash
4040
--- characters "/" in string values.
41+
--- - sort_keys: (boolean) (default false) Sort object
42+
--- keys in alphabetical order.
4143
---@return string
4244
function vim.json.encode(obj, opts) end

0 commit comments

Comments
 (0)