Skip to content

feature: add option to sort encoded object keys#115

Open
arkriny wants to merge 1 commit intoopenresty:masterfrom
arkriny:feat/sort-keys
Open

feature: add option to sort encoded object keys#115
arkriny wants to merge 1 commit intoopenresty:masterfrom
arkriny:feat/sort-keys

Conversation

@arkriny
Copy link
Copy Markdown

@arkriny arkriny commented Aug 30, 2025

Adds cjson.encode_sort_keys(enabled) option.

If enabled, keys in encoded objects will be sorted in alphabetical order.

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.

Fixes #66

@arkriny arkriny force-pushed the feat/sort-keys branch 2 times, most recently from 91af5a5 to 51bace7 Compare August 31, 2025 18:54
arkriny added a commit to arkriny/neovim that referenced this pull request 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.
@arkriny arkriny marked this pull request as ready for review August 31, 2025 19:41
arkriny added a commit to arkriny/neovim that referenced this pull request 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.
arkriny added a commit to arkriny/neovim that referenced this pull request Sep 9, 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.
@arkriny
Copy link
Copy Markdown
Author

arkriny commented Feb 9, 2026

Rebased on latest master

@justinmk
Copy link
Copy Markdown

justinmk commented Feb 9, 2026

cc @zhuizhuhaomeng

@zhuizhuhaomeng
Copy link
Copy Markdown

I will review it when I am free.

Comment thread lua_cjson.c Outdated
Comment thread lua_cjson.c Outdated
Comment thread lua_cjson.c Outdated
Comment thread lua_cjson.c
@arkriny
Copy link
Copy Markdown
Author

arkriny commented Feb 10, 2026

I changed the memory strategy: encode_keybuf is now persistent between calls (like encode_buf with encode_keep_buffer). It's only allocated or freed when the settings change. What do you think, is this better?

@zhuizhuhaomeng
Copy link
Copy Markdown

It is better, but we need to free it if it is too large.

Comment thread lua_cjson.c
Comment thread lua_cjson.c Outdated
Comment thread lua_cjson.c
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.

cjson.encode() modified order

3 participants