Skip to content

Commit 256cfd4

Browse files
authoredMar 23, 2023
IsArray option isn't parsing tags with 0 as value correctly #490 (#557)
1 parent 79a470e commit 256cfd4

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed
 

‎spec/arrayMode_spec.js

+31
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,37 @@ describe("XMLParser with arrayMode enabled", function () {
175175
expect(result).toEqual(expected);
176176
});
177177

178+
it("should parse leaf tags with zero or false value correctly in arrayMode", function () {
179+
const xmlDataExample = `
180+
<report>
181+
<value>0</value>
182+
<isNew>false</isNew>
183+
<isReport>true</isReport>
184+
</report>`
185+
186+
const expected = {
187+
"report":
188+
[
189+
{
190+
"value": 0,
191+
"isNew": false,
192+
"isReport": true
193+
}
194+
]
195+
};
196+
197+
const options = {
198+
ignoreAttributes: false,
199+
isArray: (tagName, jpath, isLeafNode, isAttribute) => {
200+
if(!isLeafNode) return true;
201+
}
202+
}
203+
const parser = new XMLParser(options);
204+
const result = parser.parse(xmlDataExample);
205+
// console.log(JSON.stringify(result,null,4));
206+
expect(result).toEqual(expected);
207+
});
208+
178209
it("should parse only attributes as Array if set", function () {
179210

180211
const expected = {

‎src/xmlparser/node2json.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,20 @@ function assignAttributes(obj, attrMap, jpath, options){
9494
}
9595

9696
function isLeafTag(obj, options){
97+
const { textNodeName } = options;
9798
const propCount = Object.keys(obj).length;
98-
if( propCount === 0 || (propCount === 1 && obj[options.textNodeName]) ) return true;
99+
100+
if (propCount === 0) {
101+
return true;
102+
}
103+
104+
if (
105+
propCount === 1 &&
106+
(obj[textNodeName] || typeof obj[textNodeName] === "boolean" || obj[textNodeName] === 0)
107+
) {
108+
return true;
109+
}
110+
99111
return false;
100112
}
101113
exports.prettify = prettify;

0 commit comments

Comments
 (0)
Please sign in to comment.