diff options
author | Wim <wim@42.be> | 2020-08-10 00:29:54 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-10 00:29:54 +0200 |
commit | 4e50fd864921c556988c919269448efdb90fa961 (patch) | |
tree | a3625f03f8de3c4f3841364000a4ea3aa42c1533 /vendor/github.com/francoispqt/gojay/encode.go | |
parent | dfdffa0027334e55ce213fc6eb62206dbf48baf6 (diff) | |
download | matterbridge-msglm-4e50fd864921c556988c919269448efdb90fa961.tar.gz matterbridge-msglm-4e50fd864921c556988c919269448efdb90fa961.tar.bz2 matterbridge-msglm-4e50fd864921c556988c919269448efdb90fa961.zip |
Use mattermost v5 module (#1192)
Diffstat (limited to 'vendor/github.com/francoispqt/gojay/encode.go')
-rw-r--r-- | vendor/github.com/francoispqt/gojay/encode.go | 202 |
1 files changed, 202 insertions, 0 deletions
diff --git a/vendor/github.com/francoispqt/gojay/encode.go b/vendor/github.com/francoispqt/gojay/encode.go new file mode 100644 index 00000000..92edaafa --- /dev/null +++ b/vendor/github.com/francoispqt/gojay/encode.go @@ -0,0 +1,202 @@ +package gojay + +import ( + "encoding/json" + "fmt" + "io" +) + +var nullBytes = []byte("null") + +// MarshalJSONArray returns the JSON encoding of v, an implementation of MarshalerJSONArray. +// +// +// Example: +// type TestSlice []*TestStruct +// +// func (t TestSlice) MarshalJSONArray(enc *Encoder) { +// for _, e := range t { +// enc.AddObject(e) +// } +// } +// +// func main() { +// test := &TestSlice{ +// &TestStruct{123456}, +// &TestStruct{7890}, +// } +// b, _ := Marshal(test) +// fmt.Println(b) // [{"id":123456},{"id":7890}] +// } +func MarshalJSONArray(v MarshalerJSONArray) ([]byte, error) { + enc := BorrowEncoder(nil) + enc.grow(512) + enc.writeByte('[') + v.(MarshalerJSONArray).MarshalJSONArray(enc) + enc.writeByte(']') + + defer func() { + enc.buf = make([]byte, 0, 512) + enc.Release() + }() + + return enc.buf, nil +} + +// MarshalJSONObject returns the JSON encoding of v, an implementation of MarshalerJSONObject. +// +// Example: +// type Object struct { +// id int +// } +// func (s *Object) MarshalJSONObject(enc *gojay.Encoder) { +// enc.IntKey("id", s.id) +// } +// func (s *Object) IsNil() bool { +// return s == nil +// } +// +// func main() { +// test := &Object{ +// id: 123456, +// } +// b, _ := gojay.Marshal(test) +// fmt.Println(b) // {"id":123456} +// } +func MarshalJSONObject(v MarshalerJSONObject) ([]byte, error) { + enc := BorrowEncoder(nil) + enc.grow(512) + + defer func() { + enc.buf = make([]byte, 0, 512) + enc.Release() + }() + + return enc.encodeObject(v) +} + +// Marshal returns the JSON encoding of v. +// +// If v is nil, not an implementation MarshalerJSONObject or MarshalerJSONArray or not one of the following types: +// string, int, int8, int16, int32, int64, uint8, uint16, uint32, uint64, float64, float32, bool +// Marshal returns an InvalidMarshalError. +func Marshal(v interface{}) ([]byte, error) { + return marshal(v, false) +} + +// MarshalAny returns the JSON encoding of v. +// +// If v is nil, not an implementation MarshalerJSONObject or MarshalerJSONArray or not one of the following types: +// string, int, int8, int16, int32, int64, uint8, uint16, uint32, uint64, float64, float32, bool +// MarshalAny falls back to "json/encoding" package to marshal the value. +func MarshalAny(v interface{}) ([]byte, error) { + return marshal(v, true) +} + +func marshal(v interface{}, any bool) ([]byte, error) { + var ( + enc = BorrowEncoder(nil) + + buf []byte + err error + ) + + defer func() { + enc.buf = make([]byte, 0, 512) + enc.Release() + }() + + buf, err = func() ([]byte, error) { + switch vt := v.(type) { + case MarshalerJSONObject: + return enc.encodeObject(vt) + case MarshalerJSONArray: + return enc.encodeArray(vt) + case string: + return enc.encodeString(vt) + case bool: + return enc.encodeBool(vt) + case int: + return enc.encodeInt(vt) + case int64: + return enc.encodeInt64(vt) + case int32: + return enc.encodeInt(int(vt)) + case int16: + return enc.encodeInt(int(vt)) + case int8: + return enc.encodeInt(int(vt)) + case uint64: + return enc.encodeInt(int(vt)) + case uint32: + return enc.encodeInt(int(vt)) + case uint16: + return enc.encodeInt(int(vt)) + case uint8: + return enc.encodeInt(int(vt)) + case float64: + return enc.encodeFloat(vt) + case float32: + return enc.encodeFloat32(vt) + case *EmbeddedJSON: + return enc.encodeEmbeddedJSON(vt) + default: + if any { + return json.Marshal(vt) + } + + return nil, InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, vt)) + } + }() + return buf, err +} + +// MarshalerJSONObject is the interface to implement for struct to be encoded +type MarshalerJSONObject interface { + MarshalJSONObject(enc *Encoder) + IsNil() bool +} + +// MarshalerJSONArray is the interface to implement +// for a slice or an array to be encoded +type MarshalerJSONArray interface { + MarshalJSONArray(enc *Encoder) + IsNil() bool +} + +// An Encoder writes JSON values to an output stream. +type Encoder struct { + buf []byte + isPooled byte + w io.Writer + err error + hasKeys bool + keys []string +} + +// AppendBytes allows a modular usage by appending bytes manually to the current state of the buffer. +func (enc *Encoder) AppendBytes(b []byte) { + enc.writeBytes(b) +} + +// AppendByte allows a modular usage by appending a single byte manually to the current state of the buffer. +func (enc *Encoder) AppendByte(b byte) { + enc.writeByte(b) +} + +// Buf returns the Encoder's buffer. +func (enc *Encoder) Buf() []byte { + return enc.buf +} + +// Write writes to the io.Writer and resets the buffer. +func (enc *Encoder) Write() (int, error) { + i, err := enc.w.Write(enc.buf) + enc.buf = enc.buf[:0] + return i, err +} + +func (enc *Encoder) getPreviousRune() byte { + last := len(enc.buf) - 1 + return enc.buf[last] +} |