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