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

The behavior of json.Unmarshal when encountering field type inconsistencies. #703

Open
ansionfor opened this issue May 30, 2024 · 0 comments

Comments

@ansionfor
Copy link

ansionfor commented May 30, 2024

func TestJson(t *testing.T) {
    type info struct {
       Id int64 `json:"id"`
    }
    type test struct {
       Result []info `json:"result"`
    }
    r := `{"result":[{"id":"1"},{"id":"2"}]}`


    var res1 test
    var res2 test


    // use encoding/json
    json2.Unmarshal([]byte(r), &res1)

    // use json-iterator/go                 
    jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(r), &res2)

    len1 := len(res1.Result)      // 2
    len2 := len(res2.Result)      // 1


    fmt.Println(len1, len2)
}

image

I want to know which handling behavior is more appropriate.

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