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

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

Open
liuq19 opened this issue Jul 9, 2024 · 1 comment
Open

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

liuq19 opened this issue Jul 9, 2024 · 1 comment

Comments

@liuq19
Copy link
Collaborator

liuq19 commented Jul 9, 2024

 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 liuq19 changed the title Unmarshal into unsupported field failed Unmarshal/Marshal for unsupported field not as encoding/json Jul 9, 2024
@liuq19
Copy link
Collaborator Author

liuq19 commented Jul 9, 2024

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)

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