marshal_json_test.go 671 B

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