github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/json/encode.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 package json 4 5 import ( 6 "encoding/base64" 7 "strconv" 8 9 "../convert" 10 ) 11 12 // Encoder store data to encode given data by each method! 13 type Encoder struct { 14 Buf []byte 15 } 16 17 // AddTrailingComma add last value in Buf as trailing comma 18 func (e *Encoder) AddTrailingComma() { 19 e.Buf = append(e.Buf, ',') 20 } 21 22 // RemoveTrailingComma remove last value in Buf as trailing comma 23 func (e *Encoder) RemoveTrailingComma() { 24 var lastItemIndex = len(e.Buf) - 1 25 if e.Buf[lastItemIndex] == ',' { 26 e.Buf = e.Buf[:lastItemIndex] 27 } 28 } 29 30 // EncodeByte append given byte to Buf 31 func (e *Encoder) EncodeByte(b byte) { 32 e.Buf = append(e.Buf, b) 33 } 34 35 // EncodeBoolean append given bool as true or false string 36 func (e *Encoder) EncodeBoolean(b bool) { 37 if b { 38 e.Buf = append(e.Buf, "true"...) 39 } else { 40 e.Buf = append(e.Buf, "false"...) 41 } 42 } 43 44 // EncodeUInt8 append given uint8 number as number string 45 // TODO::: not efficient enough code 46 func (e *Encoder) EncodeUInt8(ui uint8) { 47 e.Buf = strconv.AppendUint(e.Buf, uint64(ui), 10) 48 } 49 50 // EncodeUInt16 append given uint16 number as number string 51 // TODO::: not efficient enough code 52 func (e *Encoder) EncodeUInt16(ui uint16) { 53 e.Buf = strconv.AppendUint(e.Buf, uint64(ui), 10) 54 } 55 56 // EncodeUInt32 append given uint32 number as number string 57 func (e *Encoder) EncodeUInt32(ui uint32) { 58 e.Buf = strconv.AppendUint(e.Buf, uint64(ui), 10) 59 } 60 61 // EncodeUInt64 append given uint64 number as number string 62 func (e *Encoder) EncodeUInt64(ui uint64) { 63 e.Buf = strconv.AppendUint(e.Buf, ui, 10) 64 } 65 66 // EncodeInt64 append given int64 number as number string 67 func (e *Encoder) EncodeInt64(i int64) { 68 e.Buf = strconv.AppendInt(e.Buf, i, 10) 69 } 70 71 // EncodeFloat32 append given float32 number as number string 72 func (e *Encoder) EncodeFloat32(f float32) { 73 e.Buf = strconv.AppendFloat(e.Buf, float64(f), 'g', -1, 32) 74 } 75 76 // EncodeFloat64 append given float64 number as number string 77 func (e *Encoder) EncodeFloat64(f float64) { 78 e.Buf = strconv.AppendFloat(e.Buf, f, 'g', -1, 64) 79 } 80 81 // EncodeString append given string as given format 82 func (e *Encoder) EncodeString(s string) { 83 e.Buf = append(e.Buf, s...) 84 } 85 86 // EncodeKey append given string key as given format with "" 87 func (e *Encoder) EncodeKey(s string) { 88 e.Buf = append(e.Buf, '"') 89 e.Buf = append(e.Buf, s...) 90 e.Buf = append(e.Buf, `":`...) 91 } 92 93 // EncodeStringValue append given string as given format 94 func (e *Encoder) EncodeStringValue(s string) { 95 e.Buf = append(e.Buf, '"') 96 e.Buf = append(e.Buf, s...) 97 e.Buf = append(e.Buf, `",`...) 98 } 99 100 /* 101 Slice as Number 102 */ 103 104 // EncodeByteSliceAsNumber append given byte slice as number string 105 func (e *Encoder) EncodeByteSliceAsNumber(slice []byte) { 106 var ln = len(slice) 107 for i := 0; i < ln; i++ { 108 e.Buf = strconv.AppendUint(e.Buf, uint64(slice[i]), 10) 109 e.Buf = append(e.Buf, ',') 110 } 111 e.RemoveTrailingComma() 112 } 113 114 // EncodeUInt16SliceAsNumber append given uint16 slice as number string 115 func (e *Encoder) EncodeUInt16SliceAsNumber(slice []uint16) { 116 var ln = len(slice) 117 for i := 0; i < ln; i++ { 118 e.Buf = strconv.AppendUint(e.Buf, uint64(slice[i]), 10) 119 e.Buf = append(e.Buf, ',') 120 } 121 e.RemoveTrailingComma() 122 } 123 124 // EncodeUInt32SliceAsNumber append given uint32 slice as number string 125 func (e *Encoder) EncodeUInt32SliceAsNumber(slice []uint32) { 126 var ln = len(slice) 127 for i := 0; i < ln; i++ { 128 e.Buf = strconv.AppendUint(e.Buf, uint64(slice[i]), 10) 129 e.Buf = append(e.Buf, ',') 130 } 131 e.RemoveTrailingComma() 132 } 133 134 // EncodeUInt64SliceAsNumber append given byte slice as number string 135 func (e *Encoder) EncodeUInt64SliceAsNumber(slice []uint64) { 136 var ln = len(slice) 137 for i := 0; i < ln; i++ { 138 e.Buf = strconv.AppendUint(e.Buf, slice[i], 10) 139 e.EncodeByte(',') 140 } 141 e.RemoveTrailingComma() 142 } 143 144 /* 145 Slice as Base64 146 */ 147 148 // EncodeByteSliceAsBase64 use to append []byte as base64 string 149 func (e *Encoder) EncodeByteSliceAsBase64(slice []byte) { 150 var base64Len int = base64.RawStdEncoding.EncodedLen(len(slice)) 151 var ln = len(e.Buf) 152 e.Buf = e.Buf[:ln+base64Len] 153 base64.RawStdEncoding.Encode(e.Buf[ln:], slice) 154 } 155 156 // EncodeUInt16SliceAsBase64 use to append []byte as base64 string 157 func (e *Encoder) EncodeUInt16SliceAsBase64(slice []uint16) { 158 var base64Len int = base64.RawStdEncoding.EncodedLen(len(slice) * 2) 159 var ln = len(e.Buf) 160 e.Buf = e.Buf[:ln+base64Len] 161 base64.RawStdEncoding.Encode(e.Buf[ln:], convert.UnsafeUInt16SliceToByteSlice(slice)) 162 } 163 164 // EncodeUInt32SliceAsBase64 use to append []byte as base64 string 165 func (e *Encoder) EncodeUInt32SliceAsBase64(slice []uint32) { 166 var base64Len int = base64.RawStdEncoding.EncodedLen(len(slice) * 4) 167 var ln = len(e.Buf) 168 e.Buf = e.Buf[:ln+base64Len] 169 base64.RawStdEncoding.Encode(e.Buf[ln:], convert.UnsafeUInt32SliceToByteSlice(slice)) 170 } 171 172 // Encode32ByteArraySliceAsBase64 use to append [][32]byte as base64 string 173 func (e *Encoder) Encode32ByteArraySliceAsBase64(slice [][32]byte) { 174 const base64Len = 43 // base64.RawStdEncoding.EncodedLen(len(32)) >> (32*8 + 5) / 6 175 for _, s := range slice { 176 e.Buf = append(e.Buf, '"') 177 var ln = len(e.Buf) 178 e.Buf = e.Buf[:ln+base64Len] 179 base64.RawStdEncoding.Encode(e.Buf[ln:], s[:]) 180 e.Buf = append(e.Buf, `",`...) 181 } 182 e.RemoveTrailingComma() 183 }