bytedance / sonic

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

Unmarshal/Marshal for unsupported field not as encoding/json #670

Open liuq19 opened 1 month ago

liuq19 commented 1 month ago
 package issue_test

 import (
     "testing"
     "encoding/json"

     "github.com/bytedance/sonic"
     "github.com/stretchr/testify/assert"
 )

 type invalidField1 struct {
    A string
    P1 *chan <- int `json:"P1"`
    P2 *chan <- int `json:"P2,omitempty"`
    X1 chan <- int `json:"X1"`
    X2 chan <- int `json:"X2,omitempty"`
 }

 func TestUnmarshalInvalidField(t *testing.T) {
    for _, data := range []string{`{"A":"a","P1":null, "P2":null, "X1":null, "X2":null}`, `{"A":"a"}`} {
        var sonicv, jsonv invalidField1

        jsonerr := json.Unmarshal([]byte(data), &jsonv)
        assert.Nil(t, jsonerr)

        sonicerr := sonic.Unmarshal([]byte(data), &sonicv)
        assert.Nil(t, sonicerr)
        assert.Equal(t, sonicv, jsonv)
    }
 }

 type invalidField2 struct {
    A string
    P1 *chan <- int `json:"P1"`
    P2 *chan <- int `json:"P2,omitempty"`
 }

 func TestMarshalInvalidFieldPtr(t *testing.T) {
    var v invalidField2

    jout, jerr := json.Marshal(&v)
    assert.Nil(t, jerr)

    sout, serr := sonic.Marshal(&v)
    assert.Nil(t, serr)
    assert.Equal(t, jout, sout)
 }

 type invalidField3 struct {
    A string
    X1 chan <- int `json:"X1"`
    X2 chan <- int `json:"X2,omitempty"`
 }

 func TestMmarshalInvalidField(t *testing.T) {
    var v invalidField3

    jout, jerr := json.Marshal(&v)
    assert.NotNil(t, jerr)

    sout, serr := sonic.Marshal(&v)
    assert.NotNil(t, serr)
    assert.Equal(t, jout, sout)
 }
liuq19 commented 1 month ago

the output is

=== RUN   TestUnmarshalInvalidField
    issue670_test.go:43: 
            Error Trace:    /home/liuqiang.06/sonic/issue_test/issue670_test.go:43
            Error:          Expected nil, but got: &json.UnmarshalTypeError{Value:"", Type:(*reflect.rtype)(0x16ab900), Offset:0, Struct:"", Field:""}
            Test:           TestUnmarshalInvalidField
    issue670_test.go:44: 
            Error Trace:    /home/liuqiang.06/sonic/issue_test/issue670_test.go:44
            Error:          Not equal: 
                            expected: issue_test.invalidField1{A:"", P1:(*chan<- int)(nil), P2:(*chan<- int)(nil), X1:(chan<- int)(nil), X2:(chan<- int)(nil)}
                            actual  : issue_test.invalidField1{A:"a", P1:(*chan<- int)(nil), P2:(*chan<- int)(nil), X1:(chan<- int)(nil), X2:(chan<- int)(nil)}

                            Diff:
                            --- Expected
                            +++ Actual
                            @@ -1,3 +1,3 @@
                             (issue_test.invalidField1) {
                            - A: (string) "",
                            + A: (string) (len=1) "a",
                              P1: (*chan<- int)(<nil>),
            Test:           TestUnmarshalInvalidField
    issue670_test.go:43: 
            Error Trace:    /home/liuqiang.06/sonic/issue_test/issue670_test.go:43
            Error:          Expected nil, but got: &json.UnmarshalTypeError{Value:"", Type:(*reflect.rtype)(0x16ab900), Offset:0, Struct:"", Field:""}
            Test:           TestUnmarshalInvalidField
    issue670_test.go:44: 
            Error Trace:    /home/liuqiang.06/sonic/issue_test/issue670_test.go:44
            Error:          Not equal: 
                            expected: issue_test.invalidField1{A:"", P1:(*chan<- int)(nil), P2:(*chan<- int)(nil), X1:(chan<- int)(nil), X2:(chan<- int)(nil)}
                            actual  : issue_test.invalidField1{A:"a", P1:(*chan<- int)(nil), P2:(*chan<- int)(nil), X1:(chan<- int)(nil), X2:(chan<- int)(nil)}

                            Diff:
                            --- Expected
                            +++ Actual
                            @@ -1,3 +1,3 @@
                             (issue_test.invalidField1) {
                            - A: (string) "",
                            + A: (string) (len=1) "a",
                              P1: (*chan<- int)(<nil>),
            Test:           TestUnmarshalInvalidField
--- FAIL: TestUnmarshalInvalidField (0.00s)
=== RUN   TestMarshalInvalidFieldPtr
    issue670_test.go:62: 
            Error Trace:    /home/liuqiang.06/sonic/issue_test/issue670_test.go:62
            Error:          Expected nil, but got: &json.UnsupportedTypeError{Type:(*reflect.rtype)(0x16ab900)}
            Test:           TestMarshalInvalidFieldPtr
    issue670_test.go:63: 
            Error Trace:    /home/liuqiang.06/sonic/issue_test/issue670_test.go:63
            Error:          Not equal: 
                            expected: "{\"A\":\"\",\"P1\":null}"
                            actual  : ""

                            Diff:
                            --- Expected
                            +++ Actual
                            @@ -1 +1 @@
                            -{"A":"","P1":null}
                            +
            Test:           TestMarshalInvalidFieldPtr
--- FAIL: TestMarshalInvalidFieldPtr (0.00s)
=== RUN   TestMmarshalInvalidField
--- PASS: TestMmarshalInvalidField (0.00s)