github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/json_object_writer.go (about)

     1  package internal
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"github.com/lulzWill/go-agent/internal/jsonx"
     7  )
     8  
     9  type jsonWriter interface {
    10  	WriteJSON(buf *bytes.Buffer)
    11  }
    12  
    13  type jsonFieldsWriter struct {
    14  	buf        *bytes.Buffer
    15  	needsComma bool
    16  }
    17  
    18  func (w *jsonFieldsWriter) addKey(key string) {
    19  	if w.needsComma {
    20  		w.buf.WriteByte(',')
    21  	} else {
    22  		w.needsComma = true
    23  	}
    24  	// defensively assume that the key needs escaping:
    25  	jsonx.AppendString(w.buf, key)
    26  	w.buf.WriteByte(':')
    27  }
    28  
    29  func (w *jsonFieldsWriter) stringField(key string, val string) {
    30  	w.addKey(key)
    31  	jsonx.AppendString(w.buf, val)
    32  }
    33  
    34  func (w *jsonFieldsWriter) intField(key string, val int64) {
    35  	w.addKey(key)
    36  	jsonx.AppendInt(w.buf, val)
    37  }
    38  
    39  func (w *jsonFieldsWriter) floatField(key string, val float64) {
    40  	w.addKey(key)
    41  	jsonx.AppendFloat(w.buf, val)
    42  }
    43  
    44  func (w *jsonFieldsWriter) rawField(key string, val JSONString) {
    45  	w.addKey(key)
    46  	w.buf.WriteString(string(val))
    47  }
    48  
    49  func (w *jsonFieldsWriter) writerField(key string, val jsonWriter) {
    50  	w.addKey(key)
    51  	val.WriteJSON(w.buf)
    52  }