github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/jsoni/api_tests/marshal_json_test.go (about) 1 package test 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/json" 7 "testing" 8 9 "github.com/bingoohuang/gg/pkg/jsoni" 10 "github.com/stretchr/testify/require" 11 ) 12 13 type Foo struct { 14 Bar interface{} 15 } 16 17 func (f Foo) MarshalJSON() ([]byte, error) { 18 var buf bytes.Buffer 19 err := json.NewEncoder(&buf).Encode(f.Bar) 20 return buf.Bytes(), err 21 } 22 23 // Standard Encoder has trailing newline. 24 func TestEncodeMarshalJSON(t *testing.T) { 25 foo := Foo{ 26 Bar: 123, 27 } 28 should := require.New(t) 29 var buf, stdbuf bytes.Buffer 30 enc := jsoni.ConfigCompatibleWithStandardLibrary.NewEncoder(&buf) 31 enc.Encode(context.Background(), foo) 32 stdenc := json.NewEncoder(&stdbuf) 33 stdenc.Encode(foo) 34 should.Equal(stdbuf.Bytes(), buf.Bytes()) 35 }