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