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 5
5
"encoding/base64"
6
6
"encoding/json"
7
7
"unsafe"
8
+ "reflect"
8
9
)
9
10
10
11
type stringCodec struct {
@@ -349,7 +350,12 @@ type emptyInterfaceCodec struct {
349
350
}
350
351
351
352
func (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
+ }
353
359
}
354
360
355
361
func (codec * emptyInterfaceCodec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
Original file line number Diff line number Diff line change 5
5
"github.com/stretchr/testify/require"
6
6
"testing"
7
7
"unsafe"
8
+ "fmt"
8
9
)
9
10
10
11
func Test_write_array_of_interface (t * testing.T ) {
@@ -297,3 +298,18 @@ func Test_array_with_nothing(t *testing.T) {
297
298
should .Nil (err )
298
299
should .Equal (`[null,null]` , output )
299
300
}
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