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

would gjson can provide a params or config like UseNumber in std encoding/json lib to solve precision problem ? #351

Open
hheedat opened this issue Mar 4, 2024 · 2 comments

Comments

@hheedat
Copy link

hheedat commented Mar 4, 2024

Test code:

package main

import (
	"encoding/json"
	"github.com/tidwall/gjson"
	"testing"
)

func TestGjson(t *testing.T) {
	data := `{"id":7328334202200869666}`
	r := gjson.Parse(data)

	t.Log("r", r)
	t.Logf("r.Value() %s", r.Value())

	bytes, err := json.Marshal(r.Value())
	if err != nil {
		panic(err)
	}

	t.Log("bytes", string(bytes))
}

Print is:

=== RUN   TestGjson
    json_test.go:13: r {"id":7328334202200869666}
    json_test.go:14: r.Value() map[id:%!s(float64=7.32833420220087e+18)]
    json_test.go:21: bytes {"id":7328334202200870000}

after processing by method Value() , the number be parse as a float64 and miss precision.
maybe gjson can provide a params or config like UseNumber in std encoding/json lib in this scenario

@shaohuading
Copy link

This problem also bothers me, I hope the author can pay attention to it.

@tidwall
Copy link
Owner

tidwall commented Mar 27, 2024

I'm not sure how this would work in practice.
Please provide an example of what it would look like to use UseNumber with a gjson.Result.

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

3 participants