2021-08-30 17:14:04 +00:00
|
|
|
package json
|
|
|
|
|
2021-09-24 16:39:57 +00:00
|
|
|
// JSONMarshalFunc is used to marshal interface to JSON encoded byte slice.
|
|
|
|
// Making it package level instead of embedded in Encoder brings
|
|
|
|
// some extra efforts at importing, but avoids value copy when the functions
|
|
|
|
// of Encoder being invoked.
|
|
|
|
// DO REMEMBER to set this variable at importing, or
|
|
|
|
// you might get a nil pointer dereference panic at runtime.
|
|
|
|
var JSONMarshalFunc func(v interface{}) ([]byte, error)
|
|
|
|
|
2021-08-30 17:14:04 +00:00
|
|
|
type Encoder struct{}
|
|
|
|
|
|
|
|
// AppendKey appends a new key to the output JSON.
|
|
|
|
func (e Encoder) AppendKey(dst []byte, key string) []byte {
|
2021-09-24 16:39:57 +00:00
|
|
|
if dst[len(dst)-1] != '{' {
|
2021-08-30 17:14:04 +00:00
|
|
|
dst = append(dst, ',')
|
|
|
|
}
|
2021-09-24 16:39:57 +00:00
|
|
|
return append(e.AppendString(dst, key), ':')
|
|
|
|
}
|