You've already forked json-iterator
							
							
				mirror of
				https://github.com/json-iterator/go.git
				synced 2025-10-31 00:07:40 +02:00 
			
		
		
		
	#197 fix place holder encoder to use EncodeInterface, WriteToStream is unsafe when the real encoder is unknown
This commit is contained in:
		| @@ -164,7 +164,7 @@ func (encoder *placeholderEncoder) Encode(ptr unsafe.Pointer, stream *Stream) { | ||||
| } | ||||
|  | ||||
| func (encoder *placeholderEncoder) EncodeInterface(val interface{}, stream *Stream) { | ||||
| 	WriteToStream(val, stream, encoder) | ||||
| 	encoder.getRealEncoder().EncodeInterface(val, stream) | ||||
| } | ||||
|  | ||||
| func (encoder *placeholderEncoder) IsEmpty(ptr unsafe.Pointer) bool { | ||||
|   | ||||
| @@ -7,8 +7,23 @@ import ( | ||||
| 	"unsafe" | ||||
|  | ||||
| 	"github.com/stretchr/testify/require" | ||||
| 	"reflect" | ||||
| ) | ||||
|  | ||||
| func Test_write_empty_interface_via_placeholder(t *testing.T) { | ||||
| 	should := require.New(t) | ||||
| 	m := map[uint32]interface{}{1:"hello"} | ||||
| 	inf := reflect.ValueOf(m).MapIndex(reflect.ValueOf(uint32(1))).Interface() | ||||
| 	encoder := &placeholderEncoder{ | ||||
| 		cfg: ConfigFastest.(*frozenConfig), | ||||
| 		cacheKey: reflect.TypeOf(m).Elem(), | ||||
| 	} | ||||
| 	stream := ConfigFastest.BorrowStream(nil) | ||||
| 	encoderOfType(ConfigFastest.(*frozenConfig), reflect.TypeOf(m).Elem()) | ||||
| 	encoder.EncodeInterface(inf, stream) | ||||
| 	should.Equal(`"hello"`, string(stream.Buffer())) | ||||
| } | ||||
|  | ||||
| func Test_write_array_of_interface(t *testing.T) { | ||||
| 	should := require.New(t) | ||||
| 	array := []interface{}{"hello"} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user