Fix field floating point parsing misrepresentation

This commit fixes an issue where fields with floating points
that have zero prefixes and underscores are being parsed as
numbers.

Now those are treated as string values.

See #736
This commit is contained in:
tidwall 2024-05-03 18:10:25 -07:00
parent 20522efba9
commit 51e6862797
3 changed files with 10 additions and 5 deletions

View File

@ -184,9 +184,13 @@ func ValueOf(data string) Value {
} else if math.IsNaN(num) {
return Value{kind: Number, data: "NaN", num: nan}
}
// Make sure that this is a JSON compatible number.
// For example, "000123" and "000_123" both parse as floats but aren't
// really Numbers that can be represents in JSON.
if gjson.Valid(data) {
return Value{kind: Number, data: data, num: num}
}
if gjson.Valid(data) {
} else if gjson.Valid(data) {
data = strings.TrimSpace(data)
r := gjson.Parse(data)
switch r.Type {

View File

@ -130,5 +130,6 @@ func TestWeight(t *testing.T) {
}
func TestNumber(t *testing.T) {
assert.Assert(ValueOf("012").Num() == 12)
assert.Assert(ValueOf("12").Num() == 12)
assert.Assert(ValueOf("012").Num() == 0)
}

View File

@ -63,7 +63,7 @@ func TestList(t *testing.T) {
})
assert.Assert(names == "fellohellojellonello")
assert.Assert(datas == "123456789012")
assert.Assert(nums == 1380)
assert.Assert(nums == 1368)
names = ""
datas = ""