Skip to content

Format numbers in text the way encoding/json does - #29

Merged
zhengchun merged 1 commit into
antchfx:masterfrom
Shaisolaris:plain-number-text
Oct 14, 2026
Merged

zhengchun merged 1 commit into
antchfx:masterfrom
Shaisolaris:plain-number-text

Conversation

@Shaisolaris

Copy link
Copy Markdown
Contributor

Text nodes format JSON numbers with %v, which switches to exponent notation for many ordinary values (encoding/json only does that below 1e-6 or from 1e21 up):

doc, _ := jsonquery.Parse(strings.NewReader(`{"a":1000000,"b":123456789,"c":-0.000001}`))
jsonquery.FindOne(doc, "//a").InnerText() // "1e+06"
jsonquery.FindOne(doc, "//b").InnerText() // "1.23456789e+08"
doc.OutputXML() // ...<a>1e+06</a><b>1.23456789e+08</b><c>-1e-06</c>...

NodeNavigator.Value() already formats numbers with json.Marshal, so XPath sees 1000000 while InnerText(), OutputXML() and Node.Data show 1e+06.

This adds a small formatValue helper that formats float64 values with json.Marshal and leaves everything else as %v. It is used for text node Data, InnerText and OutputXML, so all three now match what XPath sees. Strings and booleans are unchanged.

Testing: TestLargeNumberText checks InnerText() for four numbers and checks OutputXML(). On master, three of the InnerText() checks and the OutputXML() check fail. With the change, go test . and go vet . pass. I only ran them on Go 1.25.

InnerText, OutputXML and the Data of text nodes formatted numbers with
%v, so 1000000 became 1e+06 and 123456789 became 1.23456789e+08. Format
them the way encoding/json does instead, which matches the string value
the XPath navigator already returns.
@zhengchun
zhengchun merged commit b501fd9 into antchfx:master Oct 14, 2026
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

Successfully merging this pull request may close these issues.

2 participants