-
-
Notifications
You must be signed in to change notification settings - Fork 312
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
options.arrayMode support #135
Conversation
the arrayMode option enebles all child elements to be in an array. It improves queries in NoSQL
1 similar comment
Thanks for your PR. But this feature requires more discussion. Eg what would be the expected output for different conditions and tests to ensure that we achieve all targeted features. This feature was previously implemented and removed due to lack of clarity. Please search for closed issues. |
I needed a feature to make all child elements be within an array. The default behavior adds a child as an object if its parent element has only one child:
This requires checking all the time a type of all children to make queries:
The difference matters a lot in NoSQL queries for data research (performace and not all db's ql support type checking). I had not founded any implemented feature for the arrayMode option and i've bound the fetaure to the option. Expected behavior:
|
What would be the expected output for this? <root>
<item id="a">
<sub>1</sub>
<sub>2</sub>
</item>
<item id="b">
<sub>3</sub>
</item>
</root> Can you please test your changes against sample.xml? And please add some unit tests to validate your expectations. |
describe("XMLParser", function() {
it("should parse attributes with valid names", function() {
const xmlData = `<some>valid XML data</some>`;
const expected = {
"expected": "js object"
};
let result = parser.parse(xmlData, {
attributeNamePrefix: "",
ignoreAttributes: false,
parseAttributeValue: true,
arrayMode: true
});
//console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);
result = validator.validate(xmlData);
expect(result).toBe(true);
});
}); With the test file you've created what I can understand is; input <root foo="bar">
<item id="a">
<sub>1</sub>
<sub>2</sub>
</item>
<item id="b">
<sub>3</sub>
</item>
<item2 id="b">
<sub>3</sub>
</item2>
</root> output {
root:[{
"@_foo": "bar",
item:[
{ '@_id': 'a', sub: [ 1, 2 ] },
{ '@_id': 'b', sub: [ 3 ] }
] ,
item2: [{
'@_id': 'b', sub: [ 3 ]
}]
}]
}
Can you please generate the output against sample.xml mentioned in last comment? Because it has most of the scenarios. |
ok, it is more complicated than i expexted. I'm going to close the pull request and open an issue soon |
As I previously suggested, there are closed issues on the same topic. You can take their reference too to understand more scenarios. :) |
Is there any progress? I too would like a "forceArray: true" for child elements. |
@chovy we need to finalize the expected result for each scenario. As that is not final yet, there is no progress. |
the arrayMode option enebles all child elements to be in an array. It improves queries in NoSQL