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

Add a way for users to use Number instead of float64 #21

Open
bterrier opened this issue Nov 19, 2024 · 1 comment
Open

Add a way for users to use Number instead of float64 #21

bterrier opened this issue Nov 19, 2024 · 1 comment

Comments

@bterrier
Copy link

Right now the JSON is parsed using json.Unmarshal() with no way to customize how it is parsed.

However, for some use cases it could be useful to parse with UseNumber() (https://pkg.go.dev/encoding/json#Decoder.UseNumber) so that it is possible to correctly parse numbers that would not fit in a float64.

On way it could be done would be to replace

func parse(b []byte) (*Node, error) {
	var v interface{}
	if err := json.Unmarshal(b, &v); err != nil {
		return nil, err
	}
	doc := &Node{Type: DocumentNode}
	parseValue(v, doc, 1)
	return doc, nil
}

by something like

func ParseInterface(x interface{}) (*Node, error) {
	doc := &Node{Type: DocumentNode}
	parseValue(v, doc, 1)
        return doc, nil
}

func parse(b []byte) (*Node, error) {
	var v interface{}
	if err := json.Unmarshal(b, &v); err != nil {
		return nil, err
	}
	return ParseInterface(v)
}

so that user can either call Parse() and have the current behavior, or call ParseInterface() after doing their own unmarshalling using their own Decoder.

@crone123
Copy link

I have created a pull request for this: #22

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