Skip to main content
Version: 2.8.x(Latest)

Precision Loss of Large Numbers in JSON

Problem Description

package main

import (
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
)

func main() {
str := `{"Id":1492404095703580672,"Name":"Jason"}`
strJson := gjson.New(str)
g.Dump(strJson)
}

The output is:

"{\"Id\":1492404095703580700,\"Name\":\"Jason\"}"

Solution

package main

import (
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
)

func main() {
str := `{"Id":1492404095703580672,"Name":"Jason"}`
strJson := gjson.NewWithOptions(str, gjson.Options{
StrNumber: true,
})
g.Dump(strJson)
}

The output is:

"{\"Id\":1492404095703580672,\"Name\":\"Jason\"}"