Home > Backend Development > Golang > How to Preserve 64-Bit Integer Precision When Parsing JSON in Go?

How to Preserve 64-Bit Integer Precision When Parsing JSON in Go?

Susan Sarandon
Release: 2024-12-09 11:31:16
Original
1012 people have browsed it

How to Preserve 64-Bit Integer Precision When Parsing JSON in Go?

Preserve 64-Bit Integer Values during JSON Parsing in Go

In Go, parsing JSON data containing 64-bit integer values can result in their conversion to float64 values, leading to loss of precision. This article addresses this issue by presenting two solutions to preserve the original integer values during JSON parsing.

Solution 1: Using Decoder and UseNumber

By using the Decoder type's UseNumber method, one can instruct the decoder to treat numerical values as json.Number objects instead of float64 values. json.Number can be easily converted to a uint64 or int64 type using the ParseUint or ParseInt functions respectively.

Example:

import (
    "encoding/json"
    "bytes"
    "fmt"
    "strconv"
)

body := []byte(`{"tags":[{"id":4418489049307132905},{"id":4418489049307132906}]}`)

d := json.NewDecoder(bytes.NewBuffer(body))
d.UseNumber()
var dat map[string]interface{}
err := d.Decode(&dat)

tags := dat["tags"].([]interface{})
value := tags[0].(map[string]interface{})["id"].(json.Number)
id, _ := strconv.ParseUint(string(value), 10, 64)

fmt.Println(id) // Prints 4418489049307132905
Copy after login

Solution 2: Decoding into a Custom Structure

Defining a custom structure that matches the structure of the JSON data allows the direct assignment of integer values to integer fields. This eliminates the need for further conversion or manipulation of the data.

Example:

import (
    "encoding/json"
    "fmt"
)

type Tag struct {
    Id uint64 `json:"id"`
}

type Data struct {
    Tags []Tag `json:"tags"`
}

body := []byte(`{"tags":[{"id":4418489049307132905},{"id":4418489049307132906}]}`)
var data Data
json.Unmarshal(body, &data)

fmt.Println(data.Tags[0].Id) // Logs 4418489049307132905
Copy after login

Caution:

If the JSON data originates from a source that uses JavaScript, it's worth noting that JavaScript only supports IEEE754 double precision float values and lacks true 64-bit integers. This means that parsing such JSON data in JavaScript will result in loss of precision, regardless of the method used in Go.

The above is the detailed content of How to Preserve 64-Bit Integer Precision When Parsing JSON in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template