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

     1  package jzon
     2  
     3  import (
     4  	"unsafe"
     5  )
     6  
     7  // int8 encoder
     8  type int8Encoder struct{}
     9  
    10  func (*int8Encoder) IsEmpty(ptr unsafe.Pointer) bool {
    11  	return *(*int8)(ptr) == 0
    12  }
    13  
    14  func (*int8Encoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
    15  	if ptr == nil {
    16  		s.Null()
    17  		return
    18  	}
    19  	if opts == nil || !opts.Quoted {
    20  		s.Int8(*(*int8)(ptr))
    21  	} else {
    22  		s.quotedInt8(*(*int8)(ptr))
    23  	}
    24  }
    25  
    26  // int16 encoder
    27  type int16Encoder struct{}
    28  
    29  func (*int16Encoder) IsEmpty(ptr unsafe.Pointer) bool {
    30  	return *(*int16)(ptr) == 0
    31  }
    32  
    33  func (*int16Encoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
    34  	if ptr == nil {
    35  		s.Null()
    36  		return
    37  	}
    38  	if opts == nil || !opts.Quoted {
    39  		s.Int16(*(*int16)(ptr))
    40  	} else {
    41  		s.quotedInt16(*(*int16)(ptr))
    42  	}
    43  }
    44  
    45  // int32 encoder
    46  type int32Encoder struct{}
    47  
    48  func (*int32Encoder) IsEmpty(ptr unsafe.Pointer) bool {
    49  	return *(*int32)(ptr) == 0
    50  }
    51  
    52  func (*int32Encoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
    53  	if ptr == nil {
    54  		s.Null()
    55  		return
    56  	}
    57  	if opts == nil || !opts.Quoted {
    58  		s.Int32(*(*int32)(ptr))
    59  	} else {
    60  		s.quotedInt32(*(*int32)(ptr))
    61  	}
    62  }
    63  
    64  // int64 encoder
    65  type int64Encoder struct{}
    66  
    67  func (*int64Encoder) IsEmpty(ptr unsafe.Pointer) bool {
    68  	return *(*int64)(ptr) == 0
    69  }
    70  
    71  func (*int64Encoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
    72  	if ptr == nil {
    73  		s.Null()
    74  		return
    75  	}
    76  	if opts == nil || !opts.Quoted {
    77  		s.Int64(*(*int64)(ptr))
    78  	} else {
    79  		s.quotedInt64(*(*int64)(ptr))
    80  	}
    81  }