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

feat: add jmap to simplify json unmarshal method #1

Merged
merged 1 commit into from
Aug 22, 2023

Conversation

sleagon
Copy link
Owner

@sleagon sleagon commented Aug 22, 2023

Add a simpler method to solve precision losing problem in json unmarshal method.

Here is simple example:

    val := `{"a": 9223372036854775807, "b": 1.2e8}`
    data := make(map[string]interface{})
    err := json.Unmarshal([]byte(val), &data))

   log.Println(data["a]) // => 9223372036854776000 (float64)

Just replace one line code as follows:

original:

    err := json.Unmarshal([]byte(val), &data))
    // => 9223372036854776000 (float64)

to:

    err := json.Unmarshal([]byte(val), Ptr(JMap(data)))
    // => 9223372036854775807 (int64)

whole code as follows:

    val := `{"a": 9223372036854775807, "b": 1.2e8}`
    data := make(map[string]interface{})
    err := json.Unmarshal([]byte(val), Ptr(JMap(data)))

   log.Println(data["a]) // => 9223372036854776000 (float64)

Change-Id: I5ff24233a3f471e818d9a2ac3b6b99180feea8f1
@sleagon sleagon merged commit 89bdedf into master Aug 22, 2023
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.

None yet

1 participant