-
Notifications
You must be signed in to change notification settings - Fork 33
Fix #160: Preserve CDATA content when converting XML to JSON #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Conversation
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
@bukzor
bukzor
force-pushed
the
fix-160-cdata-support
branch
from
October 22, 2025 17:06
f3653c5 to
f54a854
Compare
...JSON CDATA sections were being silently ignored during XML-to-JSON conversion because the NodeToJSON function only handled TextNode types, not CharDataNode. This caused data loss when using the -j flag on XML with CDATA sections. Changes: - Add CharDataNode handling to all text processing in jsonutil.go - Add test demonstrating CDATA content is preserved in JSON output 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
@bukzor
bukzor
force-pushed
the
fix-160-cdata-support
branch
from
October 22, 2025 17:10
f54a854 to
41aaa0e
Compare
bukzor
added a commit
to bukzor/xq
that referenced
this pull request
Oct 22, 2025
PR sibprogrammer#162 revealed that missing node type handlers in switch statements cause silent data loss - CDATA content was being dropped because CharDataNode wasn't handled. This PR adds defensive programming to prevent this entire class of bug. Changes: - Added exhaustive switch coverage for all xmlquery.NodeType values - Added exhaustive switch coverage for xml.Token and html.TokenType - Added exhaustive switch coverage for json.Token and json.Delim - Added default cases with panic() to all exhaustive switches - Added explicit documented defaults to intentionally non-exhaustive switches This follows the Go stdlib pattern (runtime/panic.go, go/types) of using panic("unreachable") for "should be impossible" states. If future xmlquery versions add new node types, or if we overlook a handler, tests will fail immediately rather than silently corrupting output. All switches now explicitly handle defaults: - 8 exhaustive switches panic on unknown values - 7 non-exhaustive switches have documented intentional behavior Added TestExhaustiveNodeTypeHandling to verify all node types are processed without panicking. All existing tests pass. This is a non-breaking change - no API modifications, only defensive additions to switch statements. Related: sibprogrammer#162
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
Changes
internal/utils/jsonutil.goto handlexmlquery.CharDataNodein all switch statements that process textTestCDATASupportdemonstrating CDATA content is preserved in JSON outputTest Plan
Example
Before:
echo '<root><![CDATA[1 & 2]]></root>' | xq -jwould output empty objectAfter:
echo '<root><![CDATA[1 & 2]]></root>' | xq -jcorrectly outputs{"root":"1 & 2"}🤖 Generated with Claude Code