NaturalIntelligence / fast-xml-parser

Validate XML, Parse XML and Build XML rapidly without C/C++ based libraries and no callback.

Home Page:https://naturalintelligence.github.io/fast-xml-parser/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Decimal point is not preserved after parsed to json.

shikeiyan opened this issue · comments

[] 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 } ] } ] } ]

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.

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

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

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

:) 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