Skip to content

Commit 820ec30

Browse files
committed
get decoder without get type first
1 parent df8295a commit 820ec30

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

reflect.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,16 @@ func (b *ctx) append(prefix string) *ctx {
5151

5252
// ReadVal copy the underlying JSON into go interface, same as json.Unmarshal
5353
func (iter *Iterator) ReadVal(obj interface{}) {
54-
typ := reflect2.TypeOf(obj)
55-
if typ.Kind() != reflect.Ptr {
56-
iter.ReportError("ReadVal", "can only unmarshal into pointer")
57-
return
54+
cacheKey := reflect2.RTypeOf(obj)
55+
decoder := iter.cfg.getDecoderFromCache(cacheKey)
56+
if decoder == nil {
57+
typ := reflect2.TypeOf(obj)
58+
if typ.Kind() != reflect.Ptr {
59+
iter.ReportError("ReadVal", "can only unmarshal into pointer")
60+
return
61+
}
62+
decoder = iter.cfg.DecoderOf(typ)
5863
}
59-
decoder := iter.cfg.DecoderOf(typ)
6064
ptr := reflect2.PtrOf(obj)
6165
if ptr == nil {
6266
iter.ReportError("ReadVal", "can not read into nil pointer")

0 commit comments

Comments
 (0)