bytedance / sonic

A blazingly fast JSON serializing & deserializing library
Apache License 2.0
6.86k stars 336 forks source link

[BUG] can't skip mismached-type value when it is a `,string`-tagged field #686

Open AsterDY opened 2 months ago

AsterDY commented 2 months ago

Reproducible codes

func testUnmarshal(t *testing.T, eo []byte, rt reflect.Type, checkobj bool) { obj := reflect.New(rt).Interface() println(string(eo)) println("sonic") es := sonic.Unmarshal(eo, obj) obj2 := reflect.New(rt).Interface() println("std") ee := json.Unmarshal(eo, obj2) assert.Equal(t, ee ==nil, es == nil, es) if checkobj { assert.Equal(t, obj2, obj) } fmt.Printf("std: %v, obj: %#v", ee, obj2) fmt.Printf("sonic error: %v, obj: %#v", es, obj) }

type IssueStringTag struct { D int form:"D" json:"D,string" query:"D" E int }

- error
              Error:        Not equal: 
                        expected: &issue_test.IssueStringTag{D:0, E:1}
                        actual  : &issue_test.IssueStringTag{D:0, E:0}

                        Diff:
                        --- Expected
                        +++ Actual
                        @@ -2,3 +2,3 @@
                          D: (int) 0,
                        - E: (int) 1
                        + E: (int) 0
                         })
        Test:           TestIssueStringTagMismatchSkip_decode
hitzhangjie commented 1 month ago

When ,string json tag defined, the json value should be {"D":"20","E":"1"}. If so, unmarshal works as expected.