--json: preserve XML element order #167
Open
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