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 wrong slice in map not as encoding/json #406

Closed
liuq19 opened this issue Apr 26, 2023 · 1 comment · Fixed by #407
Closed

unmarshal wrong slice in map not as encoding/json #406

liuq19 opened this issue Apr 26, 2023 · 1 comment · Fixed by #407

Comments

@liuq19
Copy link
Collaborator

liuq19 commented Apr 26, 2023

sonic 目前在map 场景下面,解析错误后,解析后的值未与标准库对齐。

code:

package issue_test

import (
	"testing"
    "encoding/json"
    `github.com/davecgh/go-spew/spew`
	`github.com/stretchr/testify/require`

	"github.com/bytedance/sonic"
)
var mapdata = `{
      "ptrslice": [{"id": "1"}, {"id": "2"}, {"id": "3"}, {"id": "4"}]
  }`;

  
type FooId struct {
	Id   int     `json:"id"`
}

func TestUnmarshalErrorInMapSlice(t *testing.T) {
 	var a, b map[string][]FooId
 	se := json.Unmarshal([]byte(mapdata), &a)
	je := sonic.Unmarshal([]byte(mapdata), &b)
    spew.Dump(se, a) // len(a) = 4
    spew.Dump(je, b) // len(b) = 1
	require.Equal(t, a, b);
}

var slicedata = `[{"id": "1"}, {"id": "2"}, {"id": "3"}, {"id": "4"}]`;

func TestUnmarshalErrorInSlice(t *testing.T) {
	var a, b []*FooId
	se := json.Unmarshal([]byte(slicedata), &a)
   je := sonic.Unmarshal([]byte(slicedata), &b)
   spew.Dump(se, a)
   spew.Dump(je, b)
   require.Equal(t, a, b);
}

@liuq19
Copy link
Collaborator Author

liuq19 commented Apr 26, 2023

        	Error:      	Not equal:
        	            	expected: map[string][]issue_test.FooId{"ptrslice":[]issue_test.FooId{issue_test.FooId{Id:0}, issue_test.FooId{Id:0}, issue_test.FooId{Id:0}, issue_test.FooId{Id:0}}}
        	            	actual  : map[string][]issue_test.FooId{"ptrslice":[]issue_test.FooId{issue_test.FooId{Id:0}}}

        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1,12 +1,3 @@
        	            	 (map[string][]issue_test.FooId) (len=1) {
        	            	- (string) (len=8) "ptrslice": ([]issue_test.FooId) (len=4) {
        	            	-  (issue_test.FooId) {
        	            	-   Id: (int) 0
        	            	-  },
        	            	-  (issue_test.FooId) {
        	            	-   Id: (int) 0
        	            	-  },
        	            	-  (issue_test.FooId) {
        	            	-   Id: (int) 0
        	            	-  },
        	            	+ (string) (len=8) "ptrslice": ([]issue_test.FooId) (len=1) {
        	            	   (issue_test.FooId) {
        	Test:       	TestUnmarshalErrorInMapSlice`

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

Successfully merging a pull request may close this issue.

1 participant