Open AsterDY opened 10 months ago
*http.Request has unsupported type fields like GetBody func() (io.ReadCloser, error), but the passed obj is nil, thus std think it is a nil pointer and encode it as nothing, while sonic report error when compiling the ptr type
*http.Request
GetBody func() (io.ReadCloser, error)
std
import ( "encoding/json" "net/http" "testing" "github.com/bytedance/sonic" jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" )
type Context struct { *http.Request }
func TestContext(t *testing.T) { var obj = new(Context) out, err := sonic.Marshal(obj) out1, err1 := jsoniter.Marshal(obj) out2, err2 := json.Marshal(obj) println(string(out2)) assert.Equal(t, err2 ==nil, err1 ==nil) assert.Equal(t, err2 ==nil, err ==nil) assert.Equal(t, out2, out1) assert.Equal(t, out2, out) // err = sonic.Unmarshal(out, obj) // if err != nil { // t.Fatal(err) // } }
## result
--- FAIL: TestContext (0.02s) Error: Not equal: expected: true actual : false Test: TestContext Error: Not equal: expected: []byte{0x7b, 0x7d} actual : []byte(nil)
Diff: --- Expected +++ Actual @@ -1,4 +1,2 @@ -([]uint8) (len=2) { - 00000000 7b 7d |{}| -} +([]uint8) <nil> Test: TestContext
FAIL
reason
*http.Request
has unsupported type fields likeGetBody func() (io.ReadCloser, error)
, but the passed obj is nil, thusstd
think it is a nil pointer and encode it as nothing, while sonic report error when compiling the ptr typecodes
type Context struct { *http.Request }
func TestContext(t *testing.T) { var obj = new(Context) out, err := sonic.Marshal(obj) out1, err1 := jsoniter.Marshal(obj) out2, err2 := json.Marshal(obj) println(string(out2)) assert.Equal(t, err2 ==nil, err1 ==nil) assert.Equal(t, err2 ==nil, err ==nil) assert.Equal(t, out2, out1) assert.Equal(t, out2, out) // err = sonic.Unmarshal(out, obj) // if err != nil { // t.Fatal(err) // } }
--- FAIL: TestContext (0.02s) Error: Not equal: expected: true actual : false Test: TestContext Error: Not equal: expected: []byte{0x7b, 0x7d} actual : []byte(nil)
FAIL