You've already forked json-iterator
							
							
				mirror of
				https://github.com/json-iterator/go.git
				synced 2025-10-31 00:07:40 +02:00 
			
		
		
		
	
		
			
				
	
	
		
			33 lines
		
	
	
		
			837 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			33 lines
		
	
	
		
			837 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| package extra
 | |
| 
 | |
| import (
 | |
| 	"github.com/json-iterator/go"
 | |
| 	"github.com/stretchr/testify/require"
 | |
| 	"testing"
 | |
| )
 | |
| 
 | |
| 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)
 | |
| 		output, err := jsoniter.Marshal([]byte{1, 2, 3, 23})
 | |
| 		should.NoError(err)
 | |
| 		should.Equal(`"\\x01\\x02\\x03\\x17"`, string(output))
 | |
| 		var val []byte
 | |
| 		should.NoError(jsoniter.Unmarshal(output, &val))
 | |
| 		should.Equal([]byte{1, 2, 3, 23}, val)
 | |
| 	})
 | |
| }
 |