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

Is any way to handle large integer parsing? #375

Open
ns-kliu opened this issue Nov 26, 2024 · 1 comment
Open

Is any way to handle large integer parsing? #375

ns-kliu opened this issue Nov 26, 2024 · 1 comment

Comments

@ns-kliu
Copy link

ns-kliu commented Nov 26, 2024

As #29 said, gjson is parsing integer to float64

gjson/gjson.go

Lines 68 to 69 in 4d23028

// Num is the json number
Num float64

But it's causing a side effect for my use case that accessing by gjson and sjson like this:

	s := `{"id": 3884520826190075694}`
	n := `[]`
	n, _ = sjson.Set(n, "0", gjson.Parse(s).Value())
	fmt.Println(n) // [{"id":3884520826190076000}]

Is any way to handle large integer parsing? Really appreciate it.

@ccoVeille
Copy link

Integer/float is never fun.

Float64 is the type that can fit all others.

But float64 leads to precision errors.

But here, your number fits in an int64

https://go.dev/play/p/GrdWttFHXFl

So, the code behave badly.

You have to consider this architecture logic. A number this big will be sent via the JSON, and someone will read it.

Numbers in JSON are all floats is we refer to the RFC

So, the clients might face the same imprecision with float depending on the clients implementation of the JSON parsing.

While there is a legitimate logic in thinking there is a bug that could be fixed in gjson lib, I would encourage you to reconsider your data structure and use a string to transport such a big number.

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

No branches or pull requests

2 participants