-
Notifications
You must be signed in to change notification settings - Fork 624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why is the json serialization a string and not a float type。 #323
Comments
The network transmission of strings is more accurate, and there will be no overflow. |
You can accomplish this by creating a small wrapper type: type Dollars struct{ decimal.Decimal }
func NewDollars() Dollars {
return Dollars{decimal.New(0, 0)}
}
func (d Dollars) MarshalJSON() ([]byte, error) {
bytes, err := d.Decimal.MarshalJSON()
if err != nil {
return nil, err
}
// remove the quotes since we want it to be a JSON number
return bytes[1 : len(bytes)-1], nil
} |
Just set |
Thank you all for helping @fulldog! As @iambudi mentioned please use |
I know the string doesn't overflow, but many third-party apis specify the type |
Do you think the documentation of |
I don't think I can use global variables because I only want to use them in a specific api. Global variables may affect serialization of other api json |
can we avoid global variables? |
Can you provide an api to let the consumer decide which types to serialize?
The text was updated successfully, but these errors were encountered: