-
Notifications
You must be signed in to change notification settings - Fork 1.5k
spanner: JSON number decode precision error #8669
Description
Issue
When JSON data contains a number, then decoding it to NullJson leads to loss of precision.
Test case to reproduce
func TestDecodeJsonValue(t *testing.T) {
nulljson := NullJSON{}
err := decodeValue(intProto(145688415796432520), jsonType(), &nulljson)
if err != nil {
fmt.Println(err)
}
fmt.Println(nulljson.Value)
fmt.Println(nulljson)
}
The output of the above test case is
=== RUN TestDecodeJsonValue
1.456884157964325e+17
145688415796432500
The expected output is 145688415796432520, but the returned output is 145688415796432500.
Code causing the issue
In decodeValue() function in value.go, looking at the switch case when interface is *NullJson. The code uses,
err := json.Unmarshal([]byte(x), &y)
The json package in golang by default considers the numbers in JSON as float64 values. The documentation is attached below,

Attaching the debugger output where the number is converted to float by the json package.

References
https://stackoverflow.com/questions/16946306/preserve-int64-values-when-parsing-json-in-go
https://stackoverflow.com/questions/24480835/decoding-json-int-into-string
golang/go#42246