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

Question about Handling Large Numbers in c.ShouldBind() without Scientific Notation #4118

Open
SemiThin opened this issue Dec 18, 2024 · 0 comments

Comments

@SemiThin
Copy link

SemiThin commented Dec 18, 2024

Hi Every Body,

I’ve encountered an issue when using c.ShouldBind() in the Gin framework. When binding JSON data to Go struct fields, numeric values that are too large or too small are automatically converted to scientific notation (e.g., 1.23e5 or 5.67e-3). This can lead to precision loss or formatting issues in certain cases.

For example, the following JSON:

{
"action": "someAction",
"method": "POST",
"data": {
    "id": 123000
}
}

is being bound into a Go struct like this:

type ForwardRequest struct {
  Action string         `json:"action" binding:"required"`
  Method string         `json:"method" binding:"required"`
  Data   map[string]any `json:"data" binding:"required"`
}

In the data field, the id value is interpreted as 1.23e5, which may not be the desired format in some use cases.

In Go, I’m aware that I can use the following solution to handle this issue:

d := json.NewDecoder()
d.UseNumber()

This helps preserve the exact numeric value without converting it into scientific notation.

IN goframe:
image

My Question:
Does Gin provide a similar built-in functionality or method to prevent c.ShouldBind() from converting numbers to scientific notation?

Any insights or recommendations would be greatly appreciated.

Thanks in advance!

Best regards!

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

1 participant