github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/msgfmt/jsonfmt/encoder_dynamic.go (about)

     1  package jsonfmt
     2  
     3  import (
     4  	"unsafe"
     5  	"context"
     6  	"github.com/v2pro/plz/reflect2"
     7  )
     8  
     9  type dynamicEncoder struct {
    10  	valType reflect2.Type
    11  }
    12  
    13  func (encoder *dynamicEncoder) Encode(ctx context.Context, space []byte, ptr unsafe.Pointer) []byte {
    14  	obj := encoder.valType.UnsafeIndirect(ptr)
    15  	if obj == nil {
    16  		return append(space, 'n', 'u', 'l', 'l')
    17  	}
    18  	return EncoderOf(reflect2.TypeOf(obj)).Encode(ctx, space, reflect2.PtrOf(obj))
    19  }