github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_encoder_native.go (about) 1 package jzon 2 3 import ( 4 "unsafe" 5 ) 6 7 // bool encoder 8 type boolEncoder struct{} 9 10 func (*boolEncoder) IsEmpty(ptr unsafe.Pointer) bool { 11 return !(*(*bool)(ptr)) 12 } 13 14 func (*boolEncoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) { 15 if ptr == nil { 16 s.Null() 17 return 18 } 19 quoted := (opts != nil) && opts.Quoted 20 if !quoted { 21 s.Bool(*(*bool)(ptr)) 22 return 23 } 24 if *(*bool)(ptr) { 25 s.String("true") 26 } else { 27 s.String("false") 28 } 29 } 30 31 // string encoder 32 type stringEncoder struct{} 33 34 func (*stringEncoder) IsEmpty(ptr unsafe.Pointer) bool { 35 return *(*string)(ptr) == "" 36 } 37 38 func (*stringEncoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) { 39 if ptr == nil { 40 s.Null() 41 return 42 } 43 quoted := (opts != nil) && opts.Quoted 44 if !quoted { 45 s.String(*(*string)(ptr)) 46 return 47 } 48 subStream := s.cfg.NewStreamer() 49 defer subStream.Release() 50 subStream.String(*(*string)(ptr)) 51 if subStream.Error != nil { 52 s.Error = subStream.Error 53 return 54 } 55 s.String(localByteToString(subStream.buffer)) 56 }