This repository was archived by the owner on Dec 15, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed
Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change 55 "encoding/base64"
66 "encoding/json"
77 "unsafe"
8+ "reflect"
89)
910
1011type stringCodec struct {
@@ -349,7 +350,12 @@ type emptyInterfaceCodec struct {
349350}
350351
351352func (codec * emptyInterfaceCodec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
352- * ((* interface {})(ptr )) = iter .Read ()
353+ existing := * ((* interface {})(ptr ))
354+ if existing != nil && reflect .TypeOf (existing ).Kind () == reflect .Ptr {
355+ iter .ReadVal (existing )
356+ } else {
357+ * ((* interface {})(ptr )) = iter .Read ()
358+ }
353359}
354360
355361func (codec * emptyInterfaceCodec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
Original file line number Diff line number Diff line change 55 "github.com/stretchr/testify/require"
66 "testing"
77 "unsafe"
8+ "fmt"
89)
910
1011func Test_write_array_of_interface (t * testing.T ) {
@@ -297,3 +298,18 @@ func Test_array_with_nothing(t *testing.T) {
297298 should .Nil (err )
298299 should .Equal (`[null,null]` , output )
299300}
301+
302+ func Test_unmarshal_ptr_to_interface (t * testing.T ) {
303+ type TestData struct {
304+ Name string `json:"name"`
305+ }
306+ should := require .New (t )
307+ var obj interface {} = & TestData {}
308+ err := json .Unmarshal ([]byte (`{"name":"value"}` ), & obj )
309+ should .Nil (err )
310+ should .Equal ("&{value}" , fmt .Sprintf ("%v" , obj ))
311+ obj = interface {}(& TestData {})
312+ err = Unmarshal ([]byte (`{"name":"value"}` ), & obj )
313+ should .Nil (err )
314+ should .Equal ("&{value}" , fmt .Sprintf ("%v" , obj ))
315+ }
You can’t perform that action at this time.
0 commit comments