Skip to content
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

Allow setting floating point precision in JSON #663

Merged
merged 1 commit into from
Apr 28, 2024
Merged

Conversation

mitar
Copy link
Contributor

@mitar mitar commented Apr 5, 2024

Fixes #557.

@mitar mitar mentioned this pull request Apr 5, 2024
encoder_json.go Outdated
@@ -21,6 +21,9 @@ func init() {
json.JSONMarshalFunc = func(v interface{}) ([]byte, error) {
return InterfaceMarshalFunc(v)
}
json.FloatingPointPrecisionFunc = func() int {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this func needed?

@@ -311,16 +311,20 @@ func appendFloat(dst []byte, val float64, bitSize int) []byte {
case math.IsInf(val, -1):
return append(dst, `"-Inf"`...)
}
precision := FloatingPointPrecisionFunc()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would make more sense to pass the precision as an argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I was not able to set this as a global variable because that would introduce an import loop.

If you are OK with that, I can introduce a parameter to appendFloat and then pass it everywhere it is called.

@mitar mitar force-pushed the float-precision branch 3 times, most recently from 46f6a64 to 22fc3a9 Compare April 27, 2024 22:46
@mitar
Copy link
Contributor Author

mitar commented Apr 27, 2024

@rs: Pushed with the argument.

@rs
Copy link
Owner

rs commented Apr 28, 2024

Thanks. Last thing, can you please document it in the README?

@mitar
Copy link
Contributor Author

mitar commented Apr 28, 2024

@rs: Done.

@rs rs merged commit 7d9db06 into rs:master Apr 28, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Floating Point Precision
2 participants