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

     1  package jzon
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  )
     7  
     8  type pointerEncoderBuilder struct {
     9  	encoder   *pointerEncoder
    10  	elemRType rtype
    11  }
    12  
    13  func newPointerEncoder(elemType reflect.Type) *pointerEncoderBuilder {
    14  	return &pointerEncoderBuilder{
    15  		encoder:   &pointerEncoder{},
    16  		elemRType: rtypeOfType(elemType),
    17  	}
    18  }
    19  
    20  type pointerEncoder struct {
    21  	encoder ValEncoder
    22  }
    23  
    24  func (enc *pointerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
    25  	return *(*unsafe.Pointer)(ptr) == nil
    26  }
    27  
    28  func (enc *pointerEncoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
    29  	if ptr == nil {
    30  		s.Null()
    31  		return
    32  	}
    33  	enc.encoder.Encode(*(*unsafe.Pointer)(ptr), s, opts)
    34  }