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

[BUG] unexpected quote when handling json.Marshaler and ,string tag meantime #679

Closed
AsterDY opened this issue Jul 31, 2024 · 1 comment
Closed

Comments

@AsterDY
Copy link
Collaborator

AsterDY commented Jul 31, 2024

Reproducible codes

func TestIssue670_JSONMarshaler(t *testing.T) {
	var obj = Issue670JSONMarshaler{ D: Date(time.Now().Unix()) }
	so, _ := sonic.MarshalString(obj)
	eo, _ := json.Marshal(obj)
	assert.Equal(t, string(eo), so)
	println(string(eo))
}

func TestIssue670_JSONUnmarshaler(t *testing.T) {
	// match
	eo := []byte(`{"D":"2021-08-26","E":1}`)
	et := reflect.TypeOf(Issue670JSONMarshaler{})
	testUnmarshal(t, eo, et, true)

	// mismatch
	eo = []byte(`{"D":11,"E":1}`)
	testUnmarshal(t, eo, et, true)

	// null
	eo = []byte(`{"D":null,"E":1}`)
	testUnmarshal(t, eo, et, true)
}

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)
}

Result

                 Error:      	Not equal: 
        	            	expected: "{\"delivery_date\":\"2024-07-31\"}"
        	            	actual  : "{\"delivery_date\":\"\"2024-07-31\"\"}"
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-{"delivery_date":"2024-07-31"}
        	            	+{"delivery_date":""2024-07-31""}

        	
        	Error:      	Not equal: 
        	            	expected: true
        	            	actual  : false
        	Test:       	TestIssue670_JSONUnmarshaler
        	Messages:   	"Syntax error at index 10: invalid char\n\n\t{\"D\":\"2021-08-26\",\"E\":1}\n\t..........^.............\n"
    
        	Error:      	Not equal: 
        	            	expected: &issue_test.Issue670JSONMarshaler{D:1629936000, E:1}
        	            	actual  : &issue_test.Issue670JSONMarshaler{D:2021, E:0}
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1,4 +1,4 @@
        	            	 (*issue_test.Issue670JSONMarshaler)({
        	            	- D: (issue_test.Date) 1629936000,
        	            	- E: (int) 1
        	            	+ D: (issue_test.Date) 2021,
        	            	+ E: (int) 0
        	            	 })
        	Test:       	TestIssue670_JSONUnmarshaler
  
        	Error:      	Not equal: 
        	            	expected: &issue_test.Issue670JSONMarshaler{D:0, E:1}
        	            	actual  : &issue_test.Issue670JSONMarshaler{D:0, E:0}
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -2,3 +2,3 @@
        	            	  D: (issue_test.Date) 0,
        	            	- E: (int) 1
        	            	+ E: (int) 0
        	            	 })
        	Test:       	TestIssue670_JSONUnmarshaler

Reason

@AsterDY
Copy link
Collaborator Author

AsterDY commented Aug 6, 2024

fixed by #682

@AsterDY AsterDY closed this as completed Aug 6, 2024
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