github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_encoder_json_marshaler_deprecated.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  // Deprecated
    14  type jsonMarshalerEncoder struct {
    15  	isEmpty isEmptyFunc
    16  	rtype   rtype
    17  }
    18  
    19  func (enc *jsonMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
    20  	return enc.isEmpty(ptr)
    21  }
    22  
    23  func (enc *jsonMarshalerEncoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
    24  	if s.Error != nil {
    25  		return
    26  	}
    27  	if ptr == nil {
    28  		s.Null()
    29  		return
    30  	}
    31  	obj := packEFace(enc.rtype, ptr)
    32  	marshaler := obj.(json.Marshaler)
    33  	b, err := marshaler.MarshalJSON()
    34  	if err != nil {
    35  		s.Error = err
    36  		return
    37  	}
    38  	s.Raw(b)
    39  }
    40  
    41  // Deprecated
    42  type directJSONMarshalerEncoder struct {
    43  	isEmpty isEmptyFunc
    44  	rtype   rtype
    45  }
    46  
    47  func (enc *directJSONMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
    48  	return enc.isEmpty(ptr)
    49  }
    50  
    51  func (enc *directJSONMarshalerEncoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
    52  	if s.Error != nil {
    53  		return
    54  	}
    55  	if ptr == nil {
    56  		s.Null()
    57  		return
    58  	}
    59  	obj := packEFace(enc.rtype, *(*unsafe.Pointer)(ptr))
    60  	marshaler := obj.(json.Marshaler)
    61  	b, err := marshaler.MarshalJSON()
    62  	if err != nil {
    63  		s.Error = err
    64  		return
    65  	}
    66  	s.Raw(b)
    67  }
    68  
    69  // Deprecated
    70  type pointerJSONMarshalerEncoder rtype
    71  
    72  func (enc pointerJSONMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
    73  	return *(*unsafe.Pointer)(ptr) == nil
    74  }
    75  
    76  func (enc pointerJSONMarshalerEncoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
    77  	if s.Error != nil {
    78  		return
    79  	}
    80  	if ptr == nil {
    81  		s.Null()
    82  		return
    83  	}
    84  	ptr = *(*unsafe.Pointer)(ptr)
    85  	if ptr == nil {
    86  		s.Null()
    87  		return
    88  	}
    89  	obj := packEFace(rtype(enc), ptr)
    90  	marshaler := obj.(json.Marshaler)
    91  	b, err := marshaler.MarshalJSON()
    92  	if err != nil {
    93  		s.Error = err
    94  		return
    95  	}
    96  	s.Raw(b)
    97  }
    98  
    99  // Deprecated
   100  type dynamicJSONMarshalerEncoder struct{}
   101  
   102  func (*dynamicJSONMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
   103  	return *(*json.Marshaler)(ptr) == nil
   104  }
   105  
   106  func (*dynamicJSONMarshalerEncoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
   107  	if s.Error != nil {
   108  		return
   109  	}
   110  	if ptr == nil {
   111  		s.Null()
   112  		return
   113  	}
   114  	marshaler := *(*json.Marshaler)(ptr)
   115  	if marshaler == nil {
   116  		s.Null()
   117  		return
   118  	}
   119  	b, err := marshaler.MarshalJSON()
   120  	if err != nil {
   121  		s.Error = err
   122  		return
   123  	}
   124  	s.Raw(b)
   125  }