Precision Loss of Large Numbers in JSON

Problem Description

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/encoding/gjson"
  4. "github.com/gogf/gf/v2/frame/g"
  5. )
  6. func main() {
  7. str := `{"Id":1492404095703580672,"Name":"Jason"}`
  8. strJson := gjson.New(str)
  9. g.Dump(strJson)
  10. }

The output is:

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

Solution

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/encoding/gjson"
  4. "github.com/gogf/gf/v2/frame/g"
  5. )
  6. func main() {
  7. str := `{"Id":1492404095703580672,"Name":"Jason"}`
  8. strJson := gjson.NewWithOptions(str, gjson.Options{
  9. StrNumber: true,
  10. })
  11. g.Dump(strJson)
  12. }

The output is:

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