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

Configuration to explicitly disable number parsing for specific xml tag values #661

Closed
5 of 6 tasks
kule1015 opened this issue Jun 19, 2024 · 3 comments
Closed
5 of 6 tasks

Comments

@kule1015
Copy link

kule1015 commented Jun 19, 2024

  • Are you running the latest version?
  • Have you included sample input, output, error, and expected output?
  • Have you checked if you are using correct configuration?
  • Did you try online tool?

Description

Hi there,

when parsing an XML tag that can have tag values of "A6" or "6.0", the intention is that the specific XML tag values are always handled as strings. An attempt was made to utilise the parser option "tagValueProcessor". During debugging, it was observed that within the function OrderedObjParser.parseTextData, the function OrderedObjParser.parseValue is called on the result of the callback of tagValueProcessor. The function OrderedObjParser.parseValue will parse the value "6.0" as the integer "6". However, all tag values of "evil" should be parsed as strings.

Did I miss anything or is this intended behavior? Thanks in advance for taking the time!

Input

<evil>6.0</evil>

<evil>A6</evil>

Code

import { XMLParser } from "fast-xml-parser";

const XML_PARSER_OPTIONS = {
  tagValueProcessor: (tagName: string, tagValue: string, jPath: string, hasAttributes: boolean, isLeafNode: boolean) => {
    if (tagName === "evil") {
      return String(tagValue)
    }
    return tagValue;
  },
};

const xmlParser = new XMLParser(XML_PARSER_OPTIONS);

const xml = "<evil>6.0</evil>"

const xml2 = "<evil>A6</evil>"

const parsedXml = xmlParser.parse(xml);
console.log(JSON.stringify(parsedXml))

const parsedXml2 = xmlParser.parse(xml2);
console.log(JSON.stringify(parsedXml2))

Output

{"evil":6}
{"evil":"A6"}

expected data

{"evil":"6.0"}
{"evil":"A6"}

Would you like to work on this issue?

  • Yes
  • No

Bookmark this repository for further updates. Visit SoloThought to know about recent features.

Copy link

We're glad you find this project helpful. We'll try to address this issue ASAP. You can vist https://solothought.com to know recent features. Don't forget to star this repo.

@amitguptagwl
Copy link
Member

In your case, you want o set the original value without parsing. As per docs, "If tagValueProcessor returns undefined or null then original value would be set without parsing."

Please check docs for more ways of achieving it. Please also give a try to v5. Let me know if you still need any help.

@kule1015
Copy link
Author

kule1015 commented Jul 1, 2024

Ah my mistake. Thanks a lot!

@kule1015 kule1015 closed this as completed Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants