forked from newrelic/go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_object_writer.go
64 lines (52 loc) · 1.25 KB
/
json_object_writer.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package internal
import (
"bytes"
"github.com/newrelic/go-agent/internal/jsonx"
)
type jsonWriter interface {
WriteJSON(buf *bytes.Buffer)
}
type jsonFieldsWriter struct {
buf *bytes.Buffer
needsComma bool
}
func (w *jsonFieldsWriter) addKey(key string) {
if w.needsComma {
w.buf.WriteByte(',')
} else {
w.needsComma = true
}
// defensively assume that the key needs escaping:
jsonx.AppendString(w.buf, key)
w.buf.WriteByte(':')
}
func (w *jsonFieldsWriter) stringField(key string, val string) {
w.addKey(key)
jsonx.AppendString(w.buf, val)
}
func (w *jsonFieldsWriter) intField(key string, val int64) {
w.addKey(key)
jsonx.AppendInt(w.buf, val)
}
func (w *jsonFieldsWriter) floatField(key string, val float64) {
w.addKey(key)
jsonx.AppendFloat(w.buf, val)
}
func (w *jsonFieldsWriter) boolField(key string, val bool) {
w.addKey(key)
if val {
w.buf.WriteString("true")
} else {
w.buf.WriteString("false")
}
}
func (w *jsonFieldsWriter) rawField(key string, val JSONString) {
w.addKey(key)
w.buf.WriteString(string(val))
}
func (w *jsonFieldsWriter) writerField(key string, val jsonWriter) {
w.addKey(key)
val.WriteJSON(w.buf)
}