// Package easyjson contains marshaler/unmarshaler interfaces and helper functions.
package easyjson import ( ) // Marshaler is an easyjson-compatible marshaler interface. type Marshaler interface { MarshalEasyJSON(w *jwriter.Writer) } // Marshaler is an easyjson-compatible unmarshaler interface. type Unmarshaler interface { UnmarshalEasyJSON(w *jlexer.Lexer) } // MarshalerUnmarshaler is an easyjson-compatible marshaler/unmarshaler interface. type MarshalerUnmarshaler interface { Marshaler Unmarshaler } // Optional defines an undefined-test method for a type to integrate with 'omitempty' logic. type Optional interface { IsDefined() bool } // UnknownsUnmarshaler provides a method to unmarshal unknown struct fileds and save them as you want type UnknownsUnmarshaler interface { UnmarshalUnknown(in *jlexer.Lexer, key string) } // UnknownsMarshaler provides a method to write additional struct fields type UnknownsMarshaler interface { MarshalUnknowns(w *jwriter.Writer, first bool) } func isNilInterface( interface{}) bool { return (*[2]uintptr)(unsafe.Pointer(&))[1] == 0 } // Marshal returns data as a single byte slice. Method is suboptimal as the data is likely to be copied // from a chain of smaller chunks. func ( Marshaler) ([]byte, error) { if isNilInterface() { return nullBytes, nil } := jwriter.Writer{} .MarshalEasyJSON(&) return .BuildBytes() } // MarshalToWriter marshals the data to an io.Writer. func ( Marshaler, io.Writer) ( int, error) { if isNilInterface() { return .Write(nullBytes) } := jwriter.Writer{} .MarshalEasyJSON(&) return .DumpTo() } // MarshalToHTTPResponseWriter sets Content-Length and Content-Type headers for the // http.ResponseWriter, and send the data to the writer. started will be equal to // false if an error occurred before any http.ResponseWriter methods were actually // invoked (in this case a 500 reply is possible). func ( Marshaler, http.ResponseWriter) ( bool, int, error) { if isNilInterface() { .Header().Set("Content-Type", "application/json") .Header().Set("Content-Length", strconv.Itoa(len(nullBytes))) , = .Write(nullBytes) return true, , } := jwriter.Writer{} .MarshalEasyJSON(&) if .Error != nil { return false, 0, .Error } .Header().Set("Content-Type", "application/json") .Header().Set("Content-Length", strconv.Itoa(.Size())) = true , = .DumpTo() return } // Unmarshal decodes the JSON in data into the object. func ( []byte, Unmarshaler) error { := jlexer.Lexer{Data: } .UnmarshalEasyJSON(&) return .Error() } // UnmarshalFromReader reads all the data in the reader and decodes as JSON into the object. func ( io.Reader, Unmarshaler) error { , := ioutil.ReadAll() if != nil { return } := jlexer.Lexer{Data: } .UnmarshalEasyJSON(&) return .Error() }