github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_encoder_native_uint.go (about)

     1  package jzon
     2  
     3  import (
     4  	"unsafe"
     5  )
     6  
     7  // uint8 encoder
     8  type uint8Encoder struct{}
     9  
    10  func (*uint8Encoder) IsEmpty(ptr unsafe.Pointer) bool {
    11  	if ptr == nil {
    12  		return true
    13  	}
    14  	return *(*uint8)(ptr) == 0
    15  }
    16  
    17  func (*uint8Encoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
    18  	if ptr == nil {
    19  		s.Null()
    20  		return
    21  	}
    22  	if opts == nil || !opts.Quoted {
    23  		s.Uint8(*(*uint8)(ptr))
    24  	} else {
    25  		s.quotedUint8(*(*uint8)(ptr))
    26  	}
    27  }
    28  
    29  // uint16 encoder
    30  type uint16Encoder struct{}
    31  
    32  func (*uint16Encoder) IsEmpty(ptr unsafe.Pointer) bool {
    33  	if ptr == nil {
    34  		return true
    35  	}
    36  	return *(*uint16)(ptr) == 0
    37  }
    38  
    39  func (*uint16Encoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
    40  	if ptr == nil {
    41  		s.Null()
    42  		return
    43  	}
    44  	if opts == nil || !opts.Quoted {
    45  		s.Uint16(*(*uint16)(ptr))
    46  	} else {
    47  		s.quotedUint16(*(*uint16)(ptr))
    48  	}
    49  }
    50  
    51  // uint32 encoder
    52  type uint32Encoder struct{}
    53  
    54  func (*uint32Encoder) IsEmpty(ptr unsafe.Pointer) bool {
    55  	if ptr == nil {
    56  		return true
    57  	}
    58  	return *(*uint32)(ptr) == 0
    59  }
    60  
    61  func (*uint32Encoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
    62  	if ptr == nil {
    63  		s.Null()
    64  		return
    65  	}
    66  	if opts == nil || !opts.Quoted {
    67  		s.Uint32(*(*uint32)(ptr))
    68  	} else {
    69  		s.quotedUint32(*(*uint32)(ptr))
    70  	}
    71  }
    72  
    73  // uint64 encoder
    74  type uint64Encoder struct{}
    75  
    76  func (*uint64Encoder) IsEmpty(ptr unsafe.Pointer) bool {
    77  	if ptr == nil {
    78  		return true
    79  	}
    80  	return *(*uint64)(ptr) == 0
    81  }
    82  
    83  func (*uint64Encoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
    84  	if ptr == nil {
    85  		s.Null()
    86  		return
    87  	}
    88  	if opts == nil || !opts.Quoted {
    89  		s.Uint64(*(*uint64)(ptr))
    90  	} else {
    91  		s.quotedUint64(*(*uint64)(ptr))
    92  	}
    93  }