Skip to content
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

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

Closed
4 of 6 tasks
DolarJoe opened this issue Aug 19, 2022 · 3 comments
Closed
4 of 6 tasks

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

DolarJoe opened this issue Aug 19, 2022 · 3 comments
Labels
good first issue Pending Pending to be confirmed by user/author for some check/update/implementation

Comments

@DolarJoe
Copy link

  • Are you running the latest version?
    "fast-xml-parser": "^4.0.9"
  • Have you included sample input, output, error, and expected output?
  • Have you checked if you are using correct configuration?
  • 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
  • No
@github-actions
Copy link

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.

@amitguptagwl amitguptagwl added good first issue Pending Pending to be confirmed by user/author for some check/update/implementation labels Aug 23, 2022
@omggga
Copy link
Contributor

omggga commented Mar 20, 2023

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;

@amitguptagwl
Copy link
Member

fixed as part of 4.1.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Pending Pending to be confirmed by user/author for some check/update/implementation
Projects
None yet
Development

No branches or pull requests

3 participants