github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_encoder_text_marshaler_deprecated.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 // Deprecated 14 type textMarshalerEncoder struct { 15 isEmpty isEmptyFunc 16 rtype rtype 17 } 18 19 func (enc *textMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool { 20 return enc.isEmpty(ptr) 21 } 22 23 func (enc *textMarshalerEncoder) 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.(encoding.TextMarshaler) 33 b, err := marshaler.MarshalText() 34 if err != nil { 35 s.Error = err 36 return 37 } 38 s.String(localByteToString(b)) 39 } 40 41 // Deprecated 42 type directTextMarshalerEncoder struct { 43 isEmpty isEmptyFunc 44 rtype rtype 45 } 46 47 func (enc *directTextMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool { 48 return enc.isEmpty(ptr) 49 } 50 51 func (enc *directTextMarshalerEncoder) 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.(encoding.TextMarshaler) 61 b, err := marshaler.MarshalText() 62 if err != nil { 63 s.Error = err 64 return 65 } 66 s.String(localByteToString(b)) 67 } 68 69 // Deprecated 70 type pointerTextMarshalerEncoder rtype 71 72 func (enc pointerTextMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool { 73 return *(*unsafe.Pointer)(ptr) == nil 74 } 75 76 func (enc pointerTextMarshalerEncoder) 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.(encoding.TextMarshaler) 91 b, err := marshaler.MarshalText() 92 if err != nil { 93 s.Error = err 94 return 95 } 96 s.String(localByteToString(b)) 97 } 98 99 // Deprecated 100 type dynamicTextMarshalerEncoder struct{} 101 102 func (*dynamicTextMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool { 103 return *(*encoding.TextMarshaler)(ptr) == nil 104 } 105 106 func (*dynamicTextMarshalerEncoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) { 107 if s.Error != nil { 108 return 109 } 110 marshaler := *(*encoding.TextMarshaler)(ptr) 111 if marshaler == nil { 112 s.Null() 113 return 114 } 115 b, err := marshaler.MarshalText() 116 if err != nil { 117 s.Error = err 118 return 119 } 120 s.String(localByteToString(b)) 121 }