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

Decimal point is not preserved after parsed to json. #644

Closed
shikeiyan opened this issue Mar 1, 2024 · 6 comments
Closed

Decimal point is not preserved after parsed to json. #644

shikeiyan opened this issue Mar 1, 2024 · 6 comments

Comments

@shikeiyan
Copy link

shikeiyan commented Mar 1, 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
Tried out some ways below but still not working:

  1. add options leadingZeros: false
  2. add options parseNodeValue: false
  3. upgrade to latest version

Current version used: 4.0.2

Code

const { XMLParser, XMLBuilder } = require("fast-xml-parser");
const options = {
ignoreAttributes : false,
preserveOrder: true,
trimValues: false
};

const Parser = new XMLParser(options);
const Builder = new XMLBuilder(options);

const XMLUtils = {
parse: (xmlString) => {
return Parser.parse(xmlString); // produce json
},
build: (json) => {
return Builder.build(json); // produce xml string
},
}

const xmlString = <correctResponse><value>33.00</value></correctResponse>;
const xmlJson = XMLUtils.parse(xmlString);
console.dir(xmlJson, {depth:null});

Output
[ { correctResponse: [ { value: [ { '#text': 33 } ] } ] } ]

Expected data
[ { correctResponse: [ { value: [ { '#text': 33.00 } ] } ] } ]

Copy link

github-actions bot commented Mar 1, 2024

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

@shikeiyan
Copy link
Author

Hi @amitguptagwl , I have tried to pass in options with leadingZeros: false and the decimal point is not preserved.
Refer to the attachment below, the issue is replicable in online tool as well with unchecked "parse number with leading zeros".
xml parser

@amitguptagwl
Copy link
Member

To keep a number as "33.00", it should not be parsed as number but as string only.

@shikeiyan
Copy link
Author

Hi @amitguptagwl , may I know any workaround or any fix for this on your side?

@amitguptagwl
Copy link
Member

:) This is not a bug. So no fix. In javascript, you can't represent a number like 33.00. So you need to make it as a string.

image

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