package gojayimport ()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 ( MarshalerJSONArray) ([]byte, error) { := BorrowEncoder(nil) .grow(512) .writeByte('[') .(MarshalerJSONArray).MarshalJSONArray() .writeByte(']')deferfunc() { .buf = make([]byte, 0, 512) .Release() }()return .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 ( MarshalerJSONObject) ([]byte, error) { := BorrowEncoder(nil) .grow(512)deferfunc() { .buf = make([]byte, 0, 512) .Release() }()return .encodeObject()}// 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 ( interface{}) ([]byte, error) {returnmarshal(, 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 ( interface{}) ([]byte, error) {returnmarshal(, true)}func marshal( interface{}, bool) ([]byte, error) {var ( = BorrowEncoder(nil) []byteerror )deferfunc() { .buf = make([]byte, 0, 512) .Release() }() , = func() ([]byte, error) {switch vt := .(type) {caseMarshalerJSONObject:return .encodeObject()caseMarshalerJSONArray:return .encodeArray()casestring:return .encodeString()casebool:return .encodeBool()caseint:return .encodeInt()caseint64:return .encodeInt64()caseint32:return .encodeInt(int())caseint16:return .encodeInt(int())caseint8:return .encodeInt(int())caseuint64:return .encodeInt(int())caseuint32:return .encodeInt(int())caseuint16:return .encodeInt(int())caseuint8:return .encodeInt(int())casefloat64:return .encodeFloat()casefloat32:return .encodeFloat32()case *EmbeddedJSON:return .encodeEmbeddedJSON()default:if {returnjson.Marshal() }returnnil, InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, )) } }()return , }// MarshalerJSONObject is the interface to implement for struct to be encodedtypeMarshalerJSONObjectinterface {MarshalJSONObject(enc *Encoder)IsNil() bool}// MarshalerJSONArray is the interface to implement// for a slice or an array to be encodedtypeMarshalerJSONArrayinterface {MarshalJSONArray(enc *Encoder)IsNil() bool}// An Encoder writes JSON values to an output stream.typeEncoderstruct { 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 ( *Encoder) ( []byte) { .writeBytes()}// AppendByte allows a modular usage by appending a single byte manually to the current state of the buffer.func ( *Encoder) ( byte) { .writeByte()}// Buf returns the Encoder's buffer.func ( *Encoder) () []byte {return .buf}// Write writes to the io.Writer and resets the buffer.func ( *Encoder) () (int, error) { , := .w.Write(.buf) .buf = .buf[:0]return , }func ( *Encoder) () byte { := len(.buf) - 1return .buf[]}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.