compatible_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package jsoniter
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "testing"
  6. "github.com/stretchr/testify/require"
  7. )
  8. // Standard Encoder has trailing newline.
  9. func TestEncoderHasTrailingNewline(t *testing.T) {
  10. should := require.New(t)
  11. var buf, stdbuf bytes.Buffer
  12. enc := ConfigCompatibleWithStandardLibrary.NewEncoder(&buf)
  13. enc.Encode(1)
  14. stdenc := json.NewEncoder(&stdbuf)
  15. stdenc.Encode(1)
  16. should.Equal(stdbuf.Bytes(), buf.Bytes())
  17. }
  18. // Non-nil but empty map should be ignored.
  19. func TestOmitempty(t *testing.T) {
  20. o := struct {
  21. A string `json:"a,omitempty"`
  22. B string `json:"b,omitempty"`
  23. Annotations map[string]string `json:"annotations,omitempty"`
  24. }{
  25. A: "a",
  26. B: "b",
  27. Annotations: map[string]string{},
  28. }
  29. should := require.New(t)
  30. var buf, stdbuf bytes.Buffer
  31. enc := ConfigCompatibleWithStandardLibrary.NewEncoder(&buf)
  32. enc.Encode(o)
  33. stdenc := json.NewEncoder(&stdbuf)
  34. stdenc.Encode(o)
  35. should.Equal(string(stdbuf.Bytes()), string(buf.Bytes()))
  36. }