Skip to content

Sort RFC 8785 object properties by their keys - #1100

Merged
byroot merged 1 commit into
ruby:masterfrom
ydah:fix-rfc8785-key-order
Oct 7, 2026
Merged

byroot merged 1 commit into
ruby:masterfrom
ydah:fix-rfc8785-key-order

Conversation

@ydah

@ydah ydah commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

RFC 8785 §3.2.3 requires sorting property names by their UTF-16 code units, independently of locale.

The single block argument to Hash#sort_by receives [key, value], so the current code sorts by the pair's inspected representation. This produces incorrect ordering that also depends on Encoding.default_external.

JSON.generate({ "a" => 1, "a b" => 2 }, rfc8785: true)
# Before: '{"a b":2,"a":1}'
# After:  '{"a":1,"a b":2}'

Destructure the pair to sort by the key alone.

@byroot
byroot merged commit 808577a into ruby:master Oct 7, 2026
42 checks passed
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