File tree Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change 7
7
"sync"
8
8
"unsafe"
9
9
"github.com/modern-go/concurrent"
10
+ "reflect"
10
11
)
11
12
12
13
// Config customize how the API should behave.
@@ -192,6 +193,11 @@ func (cfg *frozenConfig) validateJsonRawMessage(extension EncoderExtension) {
192
193
193
194
func (cfg * frozenConfig ) useNumber (extension DecoderExtension ) {
194
195
extension [reflect2 .TypeOfPtr ((* interface {})(nil )).Elem ()] = & funcDecoder {func (ptr unsafe.Pointer , iter * Iterator ) {
196
+ exitingValue := * ((* interface {})(ptr ))
197
+ if exitingValue != nil && reflect .TypeOf (exitingValue ).Kind () == reflect .Ptr {
198
+ iter .ReadVal (exitingValue )
199
+ return
200
+ }
195
201
if iter .WhatIsNext () == NumberValue {
196
202
* ((* interface {})(ptr )) = json .Number (iter .readNumberAsString ())
197
203
} else {
Original file line number Diff line number Diff line change @@ -130,3 +130,20 @@ func Test_reader_and_load_more(t *testing.T) {
130
130
obj := TestObject {}
131
131
should .Nil (decoder .Decode (& obj ))
132
132
}
133
+
134
+ func Test_unmarshal_into_existing_value (t * testing.T ) {
135
+ should := require .New (t )
136
+ type TestObject struct {
137
+ Field1 int
138
+ Field2 interface {}
139
+ }
140
+ var obj TestObject
141
+ m := map [string ]interface {}{}
142
+ obj .Field2 = & m
143
+ cfg := jsoniter.Config {UseNumber : true }.Froze ()
144
+ err := cfg .Unmarshal ([]byte (`{"Field1":1,"Field2":{"k":"v"}}` ), & obj )
145
+ should .NoError (err )
146
+ should .Equal (map [string ]interface {}{
147
+ "k" : "v" ,
148
+ }, m )
149
+ }
You can’t perform that action at this time.
0 commit comments