--json: preserve XML element order#167
Open
bukzor wants to merge 1 commit intosibprogrammer:masterfrom
Open
Conversation
XML element order is semantically significant and should be preserved
when converting to JSON. Previously, Go's map iteration caused keys to
be alphabetically sorted in the JSON output, losing the document order.
Changes:
- Added OrderedMap type that implements json.Marshaler
- Updated NodeToJSON to return OrderedMap instead of map[string]interface{}
- Updated nodeToJSONInternal to use OrderedMap
- Updated addToResult to work with OrderedMap
- Added TestElementOrderInJSON to verify ordering is preserved
- Updated TestCDATASupport to work with OrderedMap
- Updated formatted3.json test fixture to match element order
Example:
Input: <root><name>a</name><msg>b</msg><args>c</args></root>
Before: {"root":{"args":"c","msg":"b","name":"a"}} (alphabetical)
After: {"root":{"name":"a","msg":"b","args":"c"}} (document order)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #167 +/- ##
==========================================
+ Coverage 80.57% 80.83% +0.25%
==========================================
Files 5 5
Lines 690 720 +30
==========================================
+ Hits 556 582 +26
- Misses 92 94 +2
- Partials 42 44 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
XML element order is now preserved when converting to JSON. Previously, keys were alphabetically sorted due to Go's map iteration order.
Problem
XML element order is semantically significant. For example:
Previously, xq's JSON output lost this ordering information:
Solution
Added
OrderedMaptype that preserves insertion order by implementing customjson.Marshaler:Changes
OrderedMaptype with customMarshalJSON()implementationNodeToJSON()to return*OrderedMapinstead ofmap[string]interface{}nodeToJSONInternal()to useOrderedMapaddToResult()to work with*OrderedMapTestElementOrderInJSONto verify ordering is preservedTestCDATASupportto work with OrderedMapformatted3.jsontest fixture to match document orderBreaking Change
This is technically a breaking change for code that:
map[string]interface{}However, the previous behavior was arguably a bug, and most code should work with the new
OrderedMaptype since it marshals to standard JSON.🤖 Generated with Claude Code