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

     1  package jzon
     2  
     3  import (
     4  	"encoding/base64"
     5  	"unsafe"
     6  )
     7  
     8  type base64Encoder struct{}
     9  
    10  func (*base64Encoder) IsEmpty(ptr unsafe.Pointer) bool {
    11  	return len(*(*[]byte)(ptr)) == 0
    12  }
    13  
    14  func (*base64Encoder) Encode(ptr unsafe.Pointer, s *Streamer, _ *EncOpts) {
    15  	if s.Error != nil {
    16  		return
    17  	}
    18  	if ptr == nil {
    19  		s.Null()
    20  		return
    21  	}
    22  	src := *(*[]byte)(ptr)
    23  	if src == nil {
    24  		s.Null()
    25  		return
    26  	}
    27  	s.onVal()
    28  	s.buffer = append(s.buffer, '"')
    29  	if l := len(src); l != 0 {
    30  		enc := base64.StdEncoding
    31  		size := enc.EncodedLen(l)
    32  		buf := make([]byte, size)
    33  		enc.Encode(buf, src)
    34  		s.buffer = append(s.buffer, buf...)
    35  	}
    36  	s.buffer = append(s.buffer, '"')
    37  }