Add ensure_array_for_loops feature for XML-to-JSON compatibility#319
Open
bene-tyler wants to merge 1 commit intopantor:mainfrom
Open
Add ensure_array_for_loops feature for XML-to-JSON compatibility#319bene-tyler wants to merge 1 commit intopantor:mainfrom
bene-tyler wants to merge 1 commit intopantor:mainfrom
Conversation
Add opt-in environment flag to automatically wrap non-array values in arrays
when used in for loops. This solves the common XML-to-JSON cardinality problem
where single items are objects but multiple items are arrays.
Problem solved:
- XML with one <person> converts to object: {"person": {...}}
- XML with multiple <person> converts to array: {"person": [{...}, {...}]}
- Previously required different templates or custom ensureArray() callback
- Now no custom code is needed to use a for loop with json objects that change cardinality per request.
Usage:
env.set_ensure_array_for_loops(true);
// {% for p in person %} now works whether person is object or array
Implementation:
- Added RenderConfig::ensure_array_for_loops flag (defaults to false)
- Added Environment::set_ensure_array_for_loops() method
- Created ensure_array() helper function that wraps non-arrays:
* Arrays: returned unchanged
* Null: becomes empty array []
* Objects/primitives: wrapped in single-item array [value]
- Modified ForArrayStatementNode::visit() to call ensure_array() when enabled
Testing:
- 26 new test assertions in test-ensure-array.cpp
- Tests single objects, arrays, null, primitives, nested loops
- Tests default behavior, enabled behavior, and disabled flag
- All 284 assertions pass (26 new + 258 existing)
- No regressions in existing functionality
- Stress tested with 10,000 iterations
Backward compatibility:
- 100% backward compatible - flag defaults to false
- Without explicit enablement, behavior is identical to previous versions
- Non-arrays still throw errors by default
- Existing code requires zero changes
Documentation:
- Added usage example to README.md
- Comprehensive test suite demonstrates all use cases
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.
Add opt-in environment flag to automatically wrap non-array values in arrays when used in for loops. This solves the common XML-to-JSON cardinality problem where single items are objects but multiple items are arrays.
Problem solved:
Usage:
env.set_ensure_array_for_loops(true); // {% for p in person %} now works whether person is object or array
Implementation:
Testing:
Backward compatibility:
Documentation: