github.com/zerosnake0/jzon@v0.0.8/val_encoder_text_marshaler.go (about)

     1  package jzon
     2  
     3  import (
     4  	"encoding"
     5  	"reflect"
     6  	"unsafe"
     7  )
     8  
     9  var (
    10  	textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
    11  )
    12  
    13  type textMarshalerEncoder struct {
    14  	isEmpty isEmptyFunc
    15  	rtype   rtype
    16  }
    17  
    18  func (enc *textMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
    19  	return enc.isEmpty(ptr)
    20  }
    21  
    22  func (enc *textMarshalerEncoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
    23  	if s.Error != nil {
    24  		return
    25  	}
    26  	if ptr == nil {
    27  		s.Null()
    28  		return
    29  	}
    30  	obj := packEFace(enc.rtype, ptr)
    31  	marshaler := obj.(encoding.TextMarshaler)
    32  	b, err := marshaler.MarshalText()
    33  	if err != nil {
    34  		s.Error = err
    35  		return
    36  	}
    37  	s.String(localByteToString(b))
    38  }
    39  
    40  type directTextMarshalerEncoder struct {
    41  	isEmpty isEmptyFunc
    42  	rtype   rtype
    43  }
    44  
    45  func (enc *directTextMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
    46  	return enc.isEmpty(ptr)
    47  }
    48  
    49  func (enc *directTextMarshalerEncoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
    50  	if s.Error != nil {
    51  		return
    52  	}
    53  	if ptr == nil {
    54  		s.Null()
    55  		return
    56  	}
    57  	obj := packEFace(enc.rtype, *(*unsafe.Pointer)(ptr))
    58  	marshaler := obj.(encoding.TextMarshaler)
    59  	b, err := marshaler.MarshalText()
    60  	if err != nil {
    61  		s.Error = err
    62  		return
    63  	}
    64  	s.String(localByteToString(b))
    65  }
    66  
    67  type pointerTextMarshalerEncoder rtype
    68  
    69  func (enc pointerTextMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
    70  	return *(*unsafe.Pointer)(ptr) == nil
    71  }
    72  
    73  func (enc pointerTextMarshalerEncoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
    74  	if s.Error != nil {
    75  		return
    76  	}
    77  	if ptr == nil {
    78  		s.Null()
    79  		return
    80  	}
    81  	ptr = *(*unsafe.Pointer)(ptr)
    82  	if ptr == nil {
    83  		s.Null()
    84  		return
    85  	}
    86  	obj := packEFace(rtype(enc), ptr)
    87  	marshaler := obj.(encoding.TextMarshaler)
    88  	b, err := marshaler.MarshalText()
    89  	if err != nil {
    90  		s.Error = err
    91  		return
    92  	}
    93  	s.String(localByteToString(b))
    94  }
    95  
    96  type dynamicTextMarshalerEncoder struct{}
    97  
    98  func (*dynamicTextMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
    99  	return *(*encoding.TextMarshaler)(ptr) == nil
   100  }
   101  
   102  func (*dynamicTextMarshalerEncoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
   103  	if s.Error != nil {
   104  		return
   105  	}
   106  	marshaler := *(*encoding.TextMarshaler)(ptr)
   107  	if marshaler == nil {
   108  		s.Null()
   109  		return
   110  	}
   111  	b, err := marshaler.MarshalText()
   112  	if err != nil {
   113  		s.Error = err
   114  		return
   115  	}
   116  	s.String(localByteToString(b))
   117  }