2018-02-27 06:40:48 +02:00
|
|
|
package extra
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/json-iterator/go"
|
2018-03-15 15:28:16 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"testing"
|
2018-02-27 06:40:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
jsoniter.RegisterExtension(&BinaryAsStringExtension{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBinaryAsStringCodec(t *testing.T) {
|
|
|
|
t.Run("safe set", func(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
output, err := jsoniter.Marshal([]byte("hello"))
|
|
|
|
should.NoError(err)
|
|
|
|
should.Equal(`"hello"`, string(output))
|
|
|
|
var val []byte
|
|
|
|
should.NoError(jsoniter.Unmarshal(output, &val))
|
|
|
|
should.Equal(`hello`, string(val))
|
|
|
|
})
|
|
|
|
t.Run("non safe set", func(t *testing.T) {
|
|
|
|
should := require.New(t)
|
2021-07-19 07:58:16 +02:00
|
|
|
output, err := jsoniter.Marshal([]byte{1, 2, 3, 23})
|
2018-02-27 06:40:48 +02:00
|
|
|
should.NoError(err)
|
2021-07-19 07:58:16 +02:00
|
|
|
should.Equal(`"\\x01\\x02\\x03\\x17"`, string(output))
|
2018-02-27 06:40:48 +02:00
|
|
|
var val []byte
|
|
|
|
should.NoError(jsoniter.Unmarshal(output, &val))
|
2021-07-19 07:58:16 +02:00
|
|
|
should.Equal([]byte{1, 2, 3, 23}, val)
|
2018-02-27 06:40:48 +02:00
|
|
|
})
|
|
|
|
}
|