Home > Backend Development > Golang > How Can I Preserve Trailing Zeroes in Go\'s JSON Marshaling of Floating-Point Numbers?

How Can I Preserve Trailing Zeroes in Go\'s JSON Marshaling of Floating-Point Numbers?

Linda Hamilton
Release: 2024-11-27 20:19:10
Original
779 people have browsed it

How Can I Preserve Trailing Zeroes in Go's JSON Marshaling of Floating-Point Numbers?

JSON Marshaling: Preserving Trailing Zeroes in Floating-Point Numbers

When serializing data into JSON using json.Marshal() in Go, trailing zeroes in floating-point numbers are often stripped. This can be problematic when the receiving end expects floating-point values with decimal places.

Solution: Custom Marshal Function

An effective way to prevent json.Marshal() from removing the trailing zeroes is to define a custom MarshalJSON() method for the type containing the floating-point value. This allows you to manually control the serialization process.

Consider the following example:

type MyFloat float64

func (f MyFloat) MarshalJSON() ([]byte, error) {
    // Check if the float is an integer (no decimal part).
    if math.Trunc(float64(f)) == float64(f) {
        // Serialize as an integer without the trailing zero.
        return []byte(strconv.FormatInt(int64(f), 10)), nil
    }

    // Serialize as a floating-point number with the trailing zero.
    return []byte(strconv.FormatFloat(float64(f), 'f', -1, 64)), nil
}
Copy after login

In this example, the MyFloat type defines a custom MarshalJSON() method that checks if the float is an integer (no decimal part) and serializes it accordingly. For floating-point numbers, it serializes them with the trailing zero intact.

Usage:

Once you have defined the custom MarshalJSON() method, you can use it to serialize objects containing the MyFloat type. For instance:

type MyStruct struct {
    Value MyFloat
    Unit  string
}

// Serialize MyStruct using the custom MarshalJSON method.
data, err := json.Marshal(MyStruct{40.0, "some_string"})
Copy after login

This will result in the following JSON output:

{
  "Value": 40.0,
  "Unit": "some_string"
}
Copy after login

Note:

  • The custom MarshalJSON() method must be defined on the type that will be serialized.
  • If the float represents an integer (no decimal part), you can consider using an integer type instead of a float to avoid any ambiguity.

The above is the detailed content of How Can I Preserve Trailing Zeroes in Go\'s JSON Marshaling of Floating-Point Numbers?. 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