binary_as_string_codec_test.go 837 B

1234567891011121314151617181920212223242526272829303132
  1. package extra
  2. import (
  3. "github.com/json-iterator/go"
  4. "github.com/stretchr/testify/require"
  5. "testing"
  6. )
  7. func init() {
  8. jsoniter.RegisterExtension(&BinaryAsStringExtension{})
  9. }
  10. func TestBinaryAsStringCodec(t *testing.T) {
  11. t.Run("safe set", func(t *testing.T) {
  12. should := require.New(t)
  13. output, err := jsoniter.Marshal([]byte("hello"))
  14. should.NoError(err)
  15. should.Equal(`"hello"`, string(output))
  16. var val []byte
  17. should.NoError(jsoniter.Unmarshal(output, &val))
  18. should.Equal(`hello`, string(val))
  19. })
  20. t.Run("non safe set", func(t *testing.T) {
  21. should := require.New(t)
  22. output, err := jsoniter.Marshal([]byte{1, 2, 3, 15})
  23. should.NoError(err)
  24. should.Equal(`"\\x01\\x02\\x03\\x0f"`, string(output))
  25. var val []byte
  26. should.NoError(jsoniter.Unmarshal(output, &val))
  27. should.Equal([]byte{1, 2, 3, 15}, val)
  28. })
  29. }