Bug 24823 – std.json: Allow optionally preserving the order of fields in JSON objects
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2024-10-21T11:18:49Z
Last change time
2024-10-23T10:26:17Z
Keywords
pull
Assigned to
No Owner
Creator
Vladimir Panteleev
Comments
Comment #0 by dlang-bugzilla — 2024-10-21T11:18:49Z
The ordering of object members in JSON is explicitly undefined. Therefore, {"a": 1, "b": 2} and {"b": 2, "a": 1} are the same object.
Nevertheless, it is occasionally useful to control and preserve the order in which fields appear in JSON files. The most straight forward example is readability: sometimes, a JSON document is more readable than its equivalent document where its keys have been sorted e.g. alphabetically, as computers may not know that it makes sense to leave a "start_date" member right before the "end_date" member.
Since we already include the option to produce pretty-printed output, which is similarly not required by any specification but aids readability, such an enhancement would not be a major deviation from the current std.json feature set. Furthermore, the ability to preserve JSON field order would bring us closer to parity with other implementations, not least being current implementations of JavaScript itself, which preserves Object key order.
It should be possible, via an optional and backwards-compatible way, to request from std.json that it preserves the order of object fields in memory and during deserialization, and to obey it in such situations during serialization.
Comment #1 by dlang-bot — 2024-10-21T11:35:48Z
@CyberShadow updated dlang/phobos pull request #9064 "Fix Issue 24823 - std.json: Allow optionally preserving the order of fields in JSON objects" fixing this issue:
- Fix Bugzilla 24823 - std.json: Allow optionally preserving the order of fields in JSON objects
https://github.com/dlang/phobos/pull/9064
Comment #2 by dlang-bot — 2024-10-23T10:26:17Z
dlang/phobos pull request #9064 "Fix Issue 24823 - std.json: Allow optionally preserving the order of fields in JSON objects" was merged into master:
- dfb9db4004aba46a7d8af1728bd31b6c6bac094d by Vladimir Panteleev:
Fix Bugzilla 24823 - std.json: Allow optionally preserving the order of fields in JSON objects
https://github.com/dlang/phobos/pull/9064