fast-xml-parser icon indicating copy to clipboard operation
fast-xml-parser copied to clipboard

IsArray option isn't parsing tags with 0 as value correctly

Open DolarJoe opened this issue 2 years ago • 1 comments

  • [ ] Are you running the latest version? "fast-xml-parser": "^4.0.9"
  • [x] Have you included sample input, output, error, and expected output?
  • [x] Have you checked if you are using correct configuration?
  • [x] Did you try online tool? It seems to be broken

Description

I'd like to use IsArray option, however, it seems to break on tags which have value 0 inside of them, such as 0 or 0.0. It is inconsistent with other numbers, interger or floats, that have non 0 value, as shown in my provided example.

Input

<root>
    <a>0.00</a>
    <b>0.01</b>
</root>

Code

const parser = new XMLParser({isArray: (name, jpath, isLeaf, isAttr) => !isLeaf});
let result = parser.parse(fr.result);
console.log(JSON.stringify(result, undefined, 2))

Output

{
  "root": [
    {
      "a": [
        0
      ],
      "b": 0.01
    }
  ]
}

expected data

{
  "root": [
    {
      "a": 0,
      "b": 0.01
    }
  ]
}

Would you like to work on this issue?

  • [ ] Yes
  • [x] No

DolarJoe avatar Aug 19 '22 10:08 DolarJoe

I'm glad you find this repository helpful. I'll try to address your issue ASAP. You can watch the repo for new changes or star it.

github-actions[bot] avatar Aug 19 '22 10:08 github-actions[bot]

Yes, the same issue for me.

There is a function for it in code:

function isLeafTag(obj, options){
  const propCount = Object.keys(obj).length;
  if( propCount === 0 || (propCount === 1 && obj[options.textNodeName]) return true;
  return false;
}

https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/src/xmlparser/node2json.js#L98

The problem is that obj[options.textNodeName] is checking the value exist, but if it false or 0 it will fail the test. So you can try to fix it to test it also for boolean type or zero.

You can check it with this xml:

<root>
    <a>0</a>
    <b>false</b>
</root>

and this option:

const isArray = function(name, jpath, isLeafNode, isAttribute){ 
	if (!isLeafNode) return true
}

I see for now only one way to fix it:

if( propCount === 0 || (propCount === 1 && (obj[options.textNodeName] || typeof obj[options.textNodeName] === "boolean" || obj[options.textNodeName] === 0))) return true;

omggga avatar Mar 20 '23 07:03 omggga

fixed as part of 4.1.4

amitguptagwl avatar Apr 08 '23 06:04 amitguptagwl