github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/iterator_val.go (about)

     1  package jzon
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  )
     7  
     8  // ReadVal reads a json object and decode it to a golang object
     9  func (it *Iterator) ReadVal(obj interface{}) error {
    10  	eface := (*eface)(unsafe.Pointer(&obj))
    11  	if eface.data == nil {
    12  		return ErrNilPointerReceiver
    13  	}
    14  	dec := it.cfg.getDecoderFromCache(eface.rtype)
    15  	if dec == nil {
    16  		typ := reflect.TypeOf(obj)
    17  		if typ.Kind() != reflect.Ptr {
    18  			return ErrPointerReceiver
    19  		}
    20  		dec = it.cfg.createDecoder(eface.rtype, typ)
    21  	}
    22  	return dec.Decode(eface.data, it, nil)
    23  }