-
Notifications
You must be signed in to change notification settings - Fork 515
/
json.go
36 lines (29 loc) · 892 Bytes
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package opencost
import (
"bytes"
"fmt"
"math"
"github.com/opencost/opencost/core/pkg/util/json"
)
// TODO move everything below to a separate package
func jsonEncodeFloat64(buffer *bytes.Buffer, name string, val float64, comma string) {
var encoding string
if math.IsNaN(val) || math.IsInf(val, 0) {
encoding = fmt.Sprintf("\"%s\":null%s", name, comma)
} else {
encoding = fmt.Sprintf("\"%s\":%f%s", name, val, comma)
}
buffer.WriteString(encoding)
}
func jsonEncodeString(buffer *bytes.Buffer, name, val, comma string) {
buffer.WriteString(fmt.Sprintf("\"%s\":\"%s\"%s", name, val, comma))
}
func jsonEncode(buffer *bytes.Buffer, name string, obj interface{}, comma string) {
buffer.WriteString(fmt.Sprintf("\"%s\":", name))
if bytes, err := json.Marshal(obj); err != nil {
buffer.WriteString("null")
} else {
buffer.Write(bytes)
}
buffer.WriteString(comma)
}