Skip to content

spanner: JSON number decode precision error #8669

@harshachinta

Description

@harshachinta

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,
image

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

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

Metadata

Metadata

Labels

api: spannerIssues related to the Spanner API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions