khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame^] | 1 | package json |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "math" |
| 7 | "net" |
| 8 | "strconv" |
| 9 | ) |
| 10 | |
| 11 | // AppendNil inserts a 'Nil' object into the dst byte array. |
| 12 | func (Encoder) AppendNil(dst []byte) []byte { |
| 13 | return append(dst, "null"...) |
| 14 | } |
| 15 | |
| 16 | // AppendBeginMarker inserts a map start into the dst byte array. |
| 17 | func (Encoder) AppendBeginMarker(dst []byte) []byte { |
| 18 | return append(dst, '{') |
| 19 | } |
| 20 | |
| 21 | // AppendEndMarker inserts a map end into the dst byte array. |
| 22 | func (Encoder) AppendEndMarker(dst []byte) []byte { |
| 23 | return append(dst, '}') |
| 24 | } |
| 25 | |
| 26 | // AppendLineBreak appends a line break. |
| 27 | func (Encoder) AppendLineBreak(dst []byte) []byte { |
| 28 | return append(dst, '\n') |
| 29 | } |
| 30 | |
| 31 | // AppendArrayStart adds markers to indicate the start of an array. |
| 32 | func (Encoder) AppendArrayStart(dst []byte) []byte { |
| 33 | return append(dst, '[') |
| 34 | } |
| 35 | |
| 36 | // AppendArrayEnd adds markers to indicate the end of an array. |
| 37 | func (Encoder) AppendArrayEnd(dst []byte) []byte { |
| 38 | return append(dst, ']') |
| 39 | } |
| 40 | |
| 41 | // AppendArrayDelim adds markers to indicate end of a particular array element. |
| 42 | func (Encoder) AppendArrayDelim(dst []byte) []byte { |
| 43 | if len(dst) > 0 { |
| 44 | return append(dst, ',') |
| 45 | } |
| 46 | return dst |
| 47 | } |
| 48 | |
| 49 | // AppendBool converts the input bool to a string and |
| 50 | // appends the encoded string to the input byte slice. |
| 51 | func (Encoder) AppendBool(dst []byte, val bool) []byte { |
| 52 | return strconv.AppendBool(dst, val) |
| 53 | } |
| 54 | |
| 55 | // AppendBools encodes the input bools to json and |
| 56 | // appends the encoded string list to the input byte slice. |
| 57 | func (Encoder) AppendBools(dst []byte, vals []bool) []byte { |
| 58 | if len(vals) == 0 { |
| 59 | return append(dst, '[', ']') |
| 60 | } |
| 61 | dst = append(dst, '[') |
| 62 | dst = strconv.AppendBool(dst, vals[0]) |
| 63 | if len(vals) > 1 { |
| 64 | for _, val := range vals[1:] { |
| 65 | dst = strconv.AppendBool(append(dst, ','), val) |
| 66 | } |
| 67 | } |
| 68 | dst = append(dst, ']') |
| 69 | return dst |
| 70 | } |
| 71 | |
| 72 | // AppendInt converts the input int to a string and |
| 73 | // appends the encoded string to the input byte slice. |
| 74 | func (Encoder) AppendInt(dst []byte, val int) []byte { |
| 75 | return strconv.AppendInt(dst, int64(val), 10) |
| 76 | } |
| 77 | |
| 78 | // AppendInts encodes the input ints to json and |
| 79 | // appends the encoded string list to the input byte slice. |
| 80 | func (Encoder) AppendInts(dst []byte, vals []int) []byte { |
| 81 | if len(vals) == 0 { |
| 82 | return append(dst, '[', ']') |
| 83 | } |
| 84 | dst = append(dst, '[') |
| 85 | dst = strconv.AppendInt(dst, int64(vals[0]), 10) |
| 86 | if len(vals) > 1 { |
| 87 | for _, val := range vals[1:] { |
| 88 | dst = strconv.AppendInt(append(dst, ','), int64(val), 10) |
| 89 | } |
| 90 | } |
| 91 | dst = append(dst, ']') |
| 92 | return dst |
| 93 | } |
| 94 | |
| 95 | // AppendInt8 converts the input []int8 to a string and |
| 96 | // appends the encoded string to the input byte slice. |
| 97 | func (Encoder) AppendInt8(dst []byte, val int8) []byte { |
| 98 | return strconv.AppendInt(dst, int64(val), 10) |
| 99 | } |
| 100 | |
| 101 | // AppendInts8 encodes the input int8s to json and |
| 102 | // appends the encoded string list to the input byte slice. |
| 103 | func (Encoder) AppendInts8(dst []byte, vals []int8) []byte { |
| 104 | if len(vals) == 0 { |
| 105 | return append(dst, '[', ']') |
| 106 | } |
| 107 | dst = append(dst, '[') |
| 108 | dst = strconv.AppendInt(dst, int64(vals[0]), 10) |
| 109 | if len(vals) > 1 { |
| 110 | for _, val := range vals[1:] { |
| 111 | dst = strconv.AppendInt(append(dst, ','), int64(val), 10) |
| 112 | } |
| 113 | } |
| 114 | dst = append(dst, ']') |
| 115 | return dst |
| 116 | } |
| 117 | |
| 118 | // AppendInt16 converts the input int16 to a string and |
| 119 | // appends the encoded string to the input byte slice. |
| 120 | func (Encoder) AppendInt16(dst []byte, val int16) []byte { |
| 121 | return strconv.AppendInt(dst, int64(val), 10) |
| 122 | } |
| 123 | |
| 124 | // AppendInts16 encodes the input int16s to json and |
| 125 | // appends the encoded string list to the input byte slice. |
| 126 | func (Encoder) AppendInts16(dst []byte, vals []int16) []byte { |
| 127 | if len(vals) == 0 { |
| 128 | return append(dst, '[', ']') |
| 129 | } |
| 130 | dst = append(dst, '[') |
| 131 | dst = strconv.AppendInt(dst, int64(vals[0]), 10) |
| 132 | if len(vals) > 1 { |
| 133 | for _, val := range vals[1:] { |
| 134 | dst = strconv.AppendInt(append(dst, ','), int64(val), 10) |
| 135 | } |
| 136 | } |
| 137 | dst = append(dst, ']') |
| 138 | return dst |
| 139 | } |
| 140 | |
| 141 | // AppendInt32 converts the input int32 to a string and |
| 142 | // appends the encoded string to the input byte slice. |
| 143 | func (Encoder) AppendInt32(dst []byte, val int32) []byte { |
| 144 | return strconv.AppendInt(dst, int64(val), 10) |
| 145 | } |
| 146 | |
| 147 | // AppendInts32 encodes the input int32s to json and |
| 148 | // appends the encoded string list to the input byte slice. |
| 149 | func (Encoder) AppendInts32(dst []byte, vals []int32) []byte { |
| 150 | if len(vals) == 0 { |
| 151 | return append(dst, '[', ']') |
| 152 | } |
| 153 | dst = append(dst, '[') |
| 154 | dst = strconv.AppendInt(dst, int64(vals[0]), 10) |
| 155 | if len(vals) > 1 { |
| 156 | for _, val := range vals[1:] { |
| 157 | dst = strconv.AppendInt(append(dst, ','), int64(val), 10) |
| 158 | } |
| 159 | } |
| 160 | dst = append(dst, ']') |
| 161 | return dst |
| 162 | } |
| 163 | |
| 164 | // AppendInt64 converts the input int64 to a string and |
| 165 | // appends the encoded string to the input byte slice. |
| 166 | func (Encoder) AppendInt64(dst []byte, val int64) []byte { |
| 167 | return strconv.AppendInt(dst, val, 10) |
| 168 | } |
| 169 | |
| 170 | // AppendInts64 encodes the input int64s to json and |
| 171 | // appends the encoded string list to the input byte slice. |
| 172 | func (Encoder) AppendInts64(dst []byte, vals []int64) []byte { |
| 173 | if len(vals) == 0 { |
| 174 | return append(dst, '[', ']') |
| 175 | } |
| 176 | dst = append(dst, '[') |
| 177 | dst = strconv.AppendInt(dst, vals[0], 10) |
| 178 | if len(vals) > 1 { |
| 179 | for _, val := range vals[1:] { |
| 180 | dst = strconv.AppendInt(append(dst, ','), val, 10) |
| 181 | } |
| 182 | } |
| 183 | dst = append(dst, ']') |
| 184 | return dst |
| 185 | } |
| 186 | |
| 187 | // AppendUint converts the input uint to a string and |
| 188 | // appends the encoded string to the input byte slice. |
| 189 | func (Encoder) AppendUint(dst []byte, val uint) []byte { |
| 190 | return strconv.AppendUint(dst, uint64(val), 10) |
| 191 | } |
| 192 | |
| 193 | // AppendUints encodes the input uints to json and |
| 194 | // appends the encoded string list to the input byte slice. |
| 195 | func (Encoder) AppendUints(dst []byte, vals []uint) []byte { |
| 196 | if len(vals) == 0 { |
| 197 | return append(dst, '[', ']') |
| 198 | } |
| 199 | dst = append(dst, '[') |
| 200 | dst = strconv.AppendUint(dst, uint64(vals[0]), 10) |
| 201 | if len(vals) > 1 { |
| 202 | for _, val := range vals[1:] { |
| 203 | dst = strconv.AppendUint(append(dst, ','), uint64(val), 10) |
| 204 | } |
| 205 | } |
| 206 | dst = append(dst, ']') |
| 207 | return dst |
| 208 | } |
| 209 | |
| 210 | // AppendUint8 converts the input uint8 to a string and |
| 211 | // appends the encoded string to the input byte slice. |
| 212 | func (Encoder) AppendUint8(dst []byte, val uint8) []byte { |
| 213 | return strconv.AppendUint(dst, uint64(val), 10) |
| 214 | } |
| 215 | |
| 216 | // AppendUints8 encodes the input uint8s to json and |
| 217 | // appends the encoded string list to the input byte slice. |
| 218 | func (Encoder) AppendUints8(dst []byte, vals []uint8) []byte { |
| 219 | if len(vals) == 0 { |
| 220 | return append(dst, '[', ']') |
| 221 | } |
| 222 | dst = append(dst, '[') |
| 223 | dst = strconv.AppendUint(dst, uint64(vals[0]), 10) |
| 224 | if len(vals) > 1 { |
| 225 | for _, val := range vals[1:] { |
| 226 | dst = strconv.AppendUint(append(dst, ','), uint64(val), 10) |
| 227 | } |
| 228 | } |
| 229 | dst = append(dst, ']') |
| 230 | return dst |
| 231 | } |
| 232 | |
| 233 | // AppendUint16 converts the input uint16 to a string and |
| 234 | // appends the encoded string to the input byte slice. |
| 235 | func (Encoder) AppendUint16(dst []byte, val uint16) []byte { |
| 236 | return strconv.AppendUint(dst, uint64(val), 10) |
| 237 | } |
| 238 | |
| 239 | // AppendUints16 encodes the input uint16s to json and |
| 240 | // appends the encoded string list to the input byte slice. |
| 241 | func (Encoder) AppendUints16(dst []byte, vals []uint16) []byte { |
| 242 | if len(vals) == 0 { |
| 243 | return append(dst, '[', ']') |
| 244 | } |
| 245 | dst = append(dst, '[') |
| 246 | dst = strconv.AppendUint(dst, uint64(vals[0]), 10) |
| 247 | if len(vals) > 1 { |
| 248 | for _, val := range vals[1:] { |
| 249 | dst = strconv.AppendUint(append(dst, ','), uint64(val), 10) |
| 250 | } |
| 251 | } |
| 252 | dst = append(dst, ']') |
| 253 | return dst |
| 254 | } |
| 255 | |
| 256 | // AppendUint32 converts the input uint32 to a string and |
| 257 | // appends the encoded string to the input byte slice. |
| 258 | func (Encoder) AppendUint32(dst []byte, val uint32) []byte { |
| 259 | return strconv.AppendUint(dst, uint64(val), 10) |
| 260 | } |
| 261 | |
| 262 | // AppendUints32 encodes the input uint32s to json and |
| 263 | // appends the encoded string list to the input byte slice. |
| 264 | func (Encoder) AppendUints32(dst []byte, vals []uint32) []byte { |
| 265 | if len(vals) == 0 { |
| 266 | return append(dst, '[', ']') |
| 267 | } |
| 268 | dst = append(dst, '[') |
| 269 | dst = strconv.AppendUint(dst, uint64(vals[0]), 10) |
| 270 | if len(vals) > 1 { |
| 271 | for _, val := range vals[1:] { |
| 272 | dst = strconv.AppendUint(append(dst, ','), uint64(val), 10) |
| 273 | } |
| 274 | } |
| 275 | dst = append(dst, ']') |
| 276 | return dst |
| 277 | } |
| 278 | |
| 279 | // AppendUint64 converts the input uint64 to a string and |
| 280 | // appends the encoded string to the input byte slice. |
| 281 | func (Encoder) AppendUint64(dst []byte, val uint64) []byte { |
| 282 | return strconv.AppendUint(dst, uint64(val), 10) |
| 283 | } |
| 284 | |
| 285 | // AppendUints64 encodes the input uint64s to json and |
| 286 | // appends the encoded string list to the input byte slice. |
| 287 | func (Encoder) AppendUints64(dst []byte, vals []uint64) []byte { |
| 288 | if len(vals) == 0 { |
| 289 | return append(dst, '[', ']') |
| 290 | } |
| 291 | dst = append(dst, '[') |
| 292 | dst = strconv.AppendUint(dst, vals[0], 10) |
| 293 | if len(vals) > 1 { |
| 294 | for _, val := range vals[1:] { |
| 295 | dst = strconv.AppendUint(append(dst, ','), val, 10) |
| 296 | } |
| 297 | } |
| 298 | dst = append(dst, ']') |
| 299 | return dst |
| 300 | } |
| 301 | |
| 302 | func appendFloat(dst []byte, val float64, bitSize int) []byte { |
| 303 | // JSON does not permit NaN or Infinity. A typical JSON encoder would fail |
| 304 | // with an error, but a logging library wants the data to get thru so we |
| 305 | // make a tradeoff and store those types as string. |
| 306 | switch { |
| 307 | case math.IsNaN(val): |
| 308 | return append(dst, `"NaN"`...) |
| 309 | case math.IsInf(val, 1): |
| 310 | return append(dst, `"+Inf"`...) |
| 311 | case math.IsInf(val, -1): |
| 312 | return append(dst, `"-Inf"`...) |
| 313 | } |
| 314 | return strconv.AppendFloat(dst, val, 'f', -1, bitSize) |
| 315 | } |
| 316 | |
| 317 | // AppendFloat32 converts the input float32 to a string and |
| 318 | // appends the encoded string to the input byte slice. |
| 319 | func (Encoder) AppendFloat32(dst []byte, val float32) []byte { |
| 320 | return appendFloat(dst, float64(val), 32) |
| 321 | } |
| 322 | |
| 323 | // AppendFloats32 encodes the input float32s to json and |
| 324 | // appends the encoded string list to the input byte slice. |
| 325 | func (Encoder) AppendFloats32(dst []byte, vals []float32) []byte { |
| 326 | if len(vals) == 0 { |
| 327 | return append(dst, '[', ']') |
| 328 | } |
| 329 | dst = append(dst, '[') |
| 330 | dst = appendFloat(dst, float64(vals[0]), 32) |
| 331 | if len(vals) > 1 { |
| 332 | for _, val := range vals[1:] { |
| 333 | dst = appendFloat(append(dst, ','), float64(val), 32) |
| 334 | } |
| 335 | } |
| 336 | dst = append(dst, ']') |
| 337 | return dst |
| 338 | } |
| 339 | |
| 340 | // AppendFloat64 converts the input float64 to a string and |
| 341 | // appends the encoded string to the input byte slice. |
| 342 | func (Encoder) AppendFloat64(dst []byte, val float64) []byte { |
| 343 | return appendFloat(dst, val, 64) |
| 344 | } |
| 345 | |
| 346 | // AppendFloats64 encodes the input float64s to json and |
| 347 | // appends the encoded string list to the input byte slice. |
| 348 | func (Encoder) AppendFloats64(dst []byte, vals []float64) []byte { |
| 349 | if len(vals) == 0 { |
| 350 | return append(dst, '[', ']') |
| 351 | } |
| 352 | dst = append(dst, '[') |
| 353 | dst = appendFloat(dst, vals[0], 32) |
| 354 | if len(vals) > 1 { |
| 355 | for _, val := range vals[1:] { |
| 356 | dst = appendFloat(append(dst, ','), val, 64) |
| 357 | } |
| 358 | } |
| 359 | dst = append(dst, ']') |
| 360 | return dst |
| 361 | } |
| 362 | |
| 363 | // AppendInterface marshals the input interface to a string and |
| 364 | // appends the encoded string to the input byte slice. |
| 365 | func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte { |
| 366 | marshaled, err := json.Marshal(i) |
| 367 | if err != nil { |
| 368 | return e.AppendString(dst, fmt.Sprintf("marshaling error: %v", err)) |
| 369 | } |
| 370 | return append(dst, marshaled...) |
| 371 | } |
| 372 | |
| 373 | // AppendObjectData takes in an object that is already in a byte array |
| 374 | // and adds it to the dst. |
| 375 | func (Encoder) AppendObjectData(dst []byte, o []byte) []byte { |
| 376 | // Two conditions we want to put a ',' between existing content and |
| 377 | // new content: |
| 378 | // 1. new content starts with '{' - which shd be dropped OR |
| 379 | // 2. existing content has already other fields |
| 380 | if o[0] == '{' { |
| 381 | o[0] = ',' |
| 382 | } else if len(dst) > 1 { |
| 383 | dst = append(dst, ',') |
| 384 | } |
| 385 | return append(dst, o...) |
| 386 | } |
| 387 | |
| 388 | // AppendIPAddr adds IPv4 or IPv6 address to dst. |
| 389 | func (e Encoder) AppendIPAddr(dst []byte, ip net.IP) []byte { |
| 390 | return e.AppendString(dst, ip.String()) |
| 391 | } |
| 392 | |
| 393 | // AppendIPPrefix adds IPv4 or IPv6 Prefix (address & mask) to dst. |
| 394 | func (e Encoder) AppendIPPrefix(dst []byte, pfx net.IPNet) []byte { |
| 395 | return e.AppendString(dst, pfx.String()) |
| 396 | |
| 397 | } |
| 398 | |
| 399 | // AppendMACAddr adds MAC address to dst. |
| 400 | func (e Encoder) AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte { |
| 401 | return e.AppendString(dst, ha.String()) |
| 402 | } |