github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/issue_test/issue539_test.go (about) 1 // Copyright 2023 CloudWeGo Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package issue_test 16 17 import ( 18 `encoding/json` 19 `testing` 20 21 `github.com/bytedance/sonic` 22 `github.com/stretchr/testify/require` 23 ) 24 25 type OmitEmptyInterface struct { 26 ErrCode int32 `json:"code"` 27 Data interface{} `json:"data,omitempty"` 28 } 29 30 func TestOmitEmptyInterface(t *testing.T) { 31 // non-enmpty type 32 var data *string 33 resp := &OmitEmptyInterface{ 34 ErrCode: 123, 35 Data: data, 36 } 37 eout, eerr := json.Marshal(resp) 38 sout, serr := sonic.Marshal(resp) 39 require.Equal(t, eerr == nil, serr == nil) 40 require.Equal(t, string(eout), string(sout)) 41 42 // empty type and value 43 resp = &OmitEmptyInterface{ 44 ErrCode: 123, 45 Data: nil, 46 } 47 eout, eerr = json.Marshal(resp) 48 sout, serr = sonic.Marshal(resp) 49 require.Equal(t, eerr == nil, serr == nil) 50 require.Equal(t, string(eout), string(sout)) 51 }