Skip to content

Commit

Permalink
Added number parsing
Browse files Browse the repository at this point in the history
When config.parseNumber is set to true, all float and int values are parsed from strings
  • Loading branch information
synec committed Oct 21, 2016
1 parent 185e410 commit ed7a1ea
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion xml2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
if(config.keepCData === undefined) {
config.keepCData = false;
}
config.parseNumbers = config.parseNumbers || false;
}

var DOMNodeTypes = {
Expand Down Expand Up @@ -275,7 +276,6 @@
if(config.arrayAccessForm=="property")
delete result["#cdata-section_asArray"];
}

if( result.__cnt == 0 && config.emptyNodeForm=="text" ) {
result = '';
}
Expand All @@ -293,6 +293,13 @@
delete result.__text;
}
}
else
if (result.__cnt > 1 && result.__text!=null && /^[-+]?[0-9]*\.?[0-9]+$/.test(result.__text) && config.parseNumbers) {
result.__text = parseFloat(result.__text);
}

delete result["#text"];

delete result.__cnt;

if( config.enableToStringFunc && (result.__text!=null || result.__cdata!=null )) {
Expand Down
2 changes: 1 addition & 1 deletion xml2json.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ed7a1ea

Please sign in to comment.