You've already forked json-iterator
							
							
				mirror of
				https://github.com/json-iterator/go.git
				synced 2025-10-31 00:07:40 +02:00 
			
		
		
		
	Merge pull request #371 from nikhita/byte-base64-encode
Don't marshal empty byte or uint8 slice as null
This commit is contained in:
		| @@ -158,6 +158,27 @@ func Test_encode_byte_array(t *testing.T) { | |||||||
| 	should.Equal(`"AQID"`, string(bytes)) | 	should.Equal(`"AQID"`, string(bytes)) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func Test_encode_empty_byte_array(t *testing.T) { | ||||||
|  | 	should := require.New(t) | ||||||
|  | 	bytes, err := json.Marshal([]byte{}) | ||||||
|  | 	should.Nil(err) | ||||||
|  | 	should.Equal(`""`, string(bytes)) | ||||||
|  | 	bytes, err = jsoniter.Marshal([]byte{}) | ||||||
|  | 	should.Nil(err) | ||||||
|  | 	should.Equal(`""`, string(bytes)) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func Test_encode_nil_byte_array(t *testing.T) { | ||||||
|  | 	should := require.New(t) | ||||||
|  | 	var nilSlice []byte | ||||||
|  | 	bytes, err := json.Marshal(nilSlice) | ||||||
|  | 	should.Nil(err) | ||||||
|  | 	should.Equal(`null`, string(bytes)) | ||||||
|  | 	bytes, err = jsoniter.Marshal(nilSlice) | ||||||
|  | 	should.Nil(err) | ||||||
|  | 	should.Equal(`null`, string(bytes)) | ||||||
|  | } | ||||||
|  |  | ||||||
| func Test_decode_byte_array_from_base64(t *testing.T) { | func Test_decode_byte_array_from_base64(t *testing.T) { | ||||||
| 	should := require.New(t) | 	should := require.New(t) | ||||||
| 	data := []byte{} | 	data := []byte{} | ||||||
|   | |||||||
| @@ -432,17 +432,19 @@ func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) { | |||||||
| } | } | ||||||
|  |  | ||||||
| func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) { | func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) { | ||||||
| 	src := *((*[]byte)(ptr)) | 	if codec.sliceType.UnsafeIsNil(ptr) { | ||||||
| 	if len(src) == 0 { |  | ||||||
| 		stream.WriteNil() | 		stream.WriteNil() | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  | 	src := *((*[]byte)(ptr)) | ||||||
| 	encoding := base64.StdEncoding | 	encoding := base64.StdEncoding | ||||||
| 	stream.writeByte('"') | 	stream.writeByte('"') | ||||||
|  | 	if len(src) != 0 { | ||||||
| 		size := encoding.EncodedLen(len(src)) | 		size := encoding.EncodedLen(len(src)) | ||||||
| 		buf := make([]byte, size) | 		buf := make([]byte, size) | ||||||
| 		encoding.Encode(buf, src) | 		encoding.Encode(buf, src) | ||||||
| 		stream.buf = append(stream.buf, buf...) | 		stream.buf = append(stream.buf, buf...) | ||||||
|  | 	} | ||||||
| 	stream.writeByte('"') | 	stream.writeByte('"') | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user