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

     1  package jzon
     2  
     3  import (
     4  	"encoding/json"
     5  	"reflect"
     6  	"unsafe"
     7  )
     8  
     9  var (
    10  	jsonMarshalerType = reflect.TypeOf((*json.Marshaler)(nil)).Elem()
    11  )
    12  
    13  type jsonMarshalerEncoder struct {
    14  	isEmpty isEmptyFunc
    15  	rtype   rtype
    16  }
    17  
    18  func (enc *jsonMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
    19  	return enc.isEmpty(ptr)
    20  }
    21  
    22  func (enc *jsonMarshalerEncoder) 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.(json.Marshaler)
    32  	b, err := marshaler.MarshalJSON()
    33  	if err != nil {
    34  		s.Error = err
    35  		return
    36  	}
    37  	s.Raw(b)
    38  }
    39  
    40  type directJSONMarshalerEncoder struct {
    41  	isEmpty isEmptyFunc
    42  	rtype   rtype
    43  }
    44  
    45  func (enc *directJSONMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
    46  	return enc.isEmpty(ptr)
    47  }
    48  
    49  func (enc *directJSONMarshalerEncoder) 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.(json.Marshaler)
    59  	b, err := marshaler.MarshalJSON()
    60  	if err != nil {
    61  		s.Error = err
    62  		return
    63  	}
    64  	s.Raw(b)
    65  }
    66  
    67  type pointerJSONMarshalerEncoder rtype
    68  
    69  func (enc pointerJSONMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
    70  	return *(*unsafe.Pointer)(ptr) == nil
    71  }
    72  
    73  func (enc pointerJSONMarshalerEncoder) 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.(json.Marshaler)
    88  	b, err := marshaler.MarshalJSON()
    89  	if err != nil {
    90  		s.Error = err
    91  		return
    92  	}
    93  	s.Raw(b)
    94  }
    95  
    96  type dynamicJSONMarshalerEncoder struct{}
    97  
    98  func (*dynamicJSONMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
    99  	return *(*json.Marshaler)(ptr) == nil
   100  }
   101  
   102  func (*dynamicJSONMarshalerEncoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
   103  	if s.Error != nil {
   104  		return
   105  	}
   106  	if ptr == nil {
   107  		s.Null()
   108  		return
   109  	}
   110  	marshaler := *(*json.Marshaler)(ptr)
   111  	if marshaler == nil {
   112  		s.Null()
   113  		return
   114  	}
   115  	b, err := marshaler.MarshalJSON()
   116  	if err != nil {
   117  		s.Error = err
   118  		return
   119  	}
   120  	s.Raw(b)
   121  }