encoder_18_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //+build go1.8
  2. package test
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "testing"
  7. "unicode/utf8"
  8. "github.com/json-iterator/go"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func Test_new_encoder(t *testing.T) {
  12. should := require.New(t)
  13. buf1 := &bytes.Buffer{}
  14. encoder1 := json.NewEncoder(buf1)
  15. encoder1.SetEscapeHTML(false)
  16. encoder1.Encode([]int{1})
  17. should.Equal("[1]\n", buf1.String())
  18. buf2 := &bytes.Buffer{}
  19. encoder2 := jsoniter.NewEncoder(buf2)
  20. encoder2.SetEscapeHTML(false)
  21. encoder2.Encode([]int{1})
  22. should.Equal("[1]\n", buf2.String())
  23. }
  24. func Test_string_encode_with_std_without_html_escape(t *testing.T) {
  25. api := jsoniter.Config{EscapeHTML: false}.Froze()
  26. should := require.New(t)
  27. for i := 0; i < utf8.RuneSelf; i++ {
  28. input := string([]byte{byte(i)})
  29. buf := &bytes.Buffer{}
  30. encoder := json.NewEncoder(buf)
  31. encoder.SetEscapeHTML(false)
  32. err := encoder.Encode(input)
  33. should.Nil(err)
  34. stdOutput := buf.String()
  35. stdOutput = stdOutput[:len(stdOutput)-1]
  36. jsoniterOutputBytes, err := api.Marshal(input)
  37. should.Nil(err)
  38. jsoniterOutput := string(jsoniterOutputBytes)
  39. should.Equal(stdOutput, jsoniterOutput)
  40. }
  41. }