github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/msgfmt/jsonfmt/encoder_json_marshaler.go (about) 1 package jsonfmt 2 3 import ( 4 "unsafe" 5 "encoding/json" 6 "context" 7 "github.com/v2pro/plz/reflect2" 8 ) 9 10 type jsonMarshalerEncoder struct { 11 valType reflect2.Type 12 } 13 14 func (encoder *jsonMarshalerEncoder) Encode(ctx context.Context, space []byte, ptr unsafe.Pointer) []byte { 15 obj := encoder.valType.UnsafeIndirect(ptr) 16 buf, err := obj.(json.Marshaler).MarshalJSON() 17 if err != nil { 18 space = append(space, '"') 19 space = append(space, err.Error()...) 20 space = append(space, '"') 21 return space 22 } 23 space = append(space, buf...) 24 return space 25 }