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