Home > Backend Development > Golang > How to Preserve Trailing Zeros in JSON Output for Go\'s Floating-Point Numbers?

How to Preserve Trailing Zeros in JSON Output for Go\'s Floating-Point Numbers?

DDD
Release: 2024-11-28 11:56:14
Original
472 people have browsed it

How to Preserve Trailing Zeros in JSON Output for Go's Floating-Point Numbers?

Preserving Trailing Zeros in JSON Output for Floating-Point Numbers

In Go, the json.Marshal() function is commonly used for serializing data structures into JSON format. However, it has a tendency to strip trailing zeros from floating-point numbers during the conversion process. This can be a problem if the consuming application expects the numbers to have trailing zeros.

To address this issue, one approach is to create a custom floating-point type that implements the json.Marshaler interface. This allows you to define how the type is serialized into JSON. Here's an example implementation:

type KeepZero float64

func (f KeepZero) MarshalJSON() ([]byte, error) {
    if float64(f) == float64(int(f)) {
        return []byte(strconv.FormatFloat(float64(f), 'f', 1, 32)), nil
    }
    return []byte(strconv.FormatFloat(float64(f), 'f', -1, 32)), nil
}
Copy after login

In this code:

  • The KeepZero type is a new floating-point type that wraps the native float64 type.
  • The MarshalJSON method implements the json.Marshaler interface for the KeepZero type.
  • The strconv.FormatFloat function is used to format the floating-point number as a string. If the number is an integer (i.e., it has no fractional part), the string is formatted with a single decimal place to preserve the trailing zero. Otherwise, the string is formatted without a fixed decimal point.

To use the KeepZero type, you can replace the original float64 field in your data structure with a KeepZero field. For example:

type Pt struct {
    Value KeepZero
    Unit  string
}
Copy after login

When you call json.Marshal on a Pt object, the Value field will be serialized using the custom MarshalJSON method, preserving the trailing zero if necessary.

data, err := json.Marshal(Pt{40.0, "some_string"})
fmt.Println(string(data), err)
Copy after login

This will produce the following JSON output:

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

This solution allows you to retain the trailing zeros in your floating-point numbers when serializing them to JSON, as required by the consuming application.

The above is the detailed content of How to Preserve Trailing Zeros in JSON Output for Go\'s 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template