package easyjson
import (
"io"
"io/ioutil"
"net/http"
"strconv"
"unsafe"
"github.com/mailru/easyjson/jlexer"
"github.com/mailru/easyjson/jwriter"
)
type Marshaler interface {
MarshalEasyJSON (w *jwriter .Writer )
}
type Unmarshaler interface {
UnmarshalEasyJSON (w *jlexer .Lexer )
}
type MarshalerUnmarshaler interface {
Marshaler
Unmarshaler
}
type Optional interface {
IsDefined () bool
}
type UnknownsUnmarshaler interface {
UnmarshalUnknown (in *jlexer .Lexer , key string )
}
type UnknownsMarshaler interface {
MarshalUnknowns (w *jwriter .Writer , first bool )
}
func isNilInterface(i interface {}) bool {
return (*[2 ]uintptr )(unsafe .Pointer (&i ))[1 ] == 0
}
func Marshal (v Marshaler ) ([]byte , error ) {
if isNilInterface (v ) {
return nullBytes , nil
}
w := jwriter .Writer {}
v .MarshalEasyJSON (&w )
return w .BuildBytes ()
}
func MarshalToWriter (v Marshaler , w io .Writer ) (written int , err error ) {
if isNilInterface (v ) {
return w .Write (nullBytes )
}
jw := jwriter .Writer {}
v .MarshalEasyJSON (&jw )
return jw .DumpTo (w )
}
func MarshalToHTTPResponseWriter (v Marshaler , w http .ResponseWriter ) (started bool , written int , err error ) {
if isNilInterface (v ) {
w .Header ().Set ("Content-Type" , "application/json" )
w .Header ().Set ("Content-Length" , strconv .Itoa (len (nullBytes )))
written , err = w .Write (nullBytes )
return true , written , err
}
jw := jwriter .Writer {}
v .MarshalEasyJSON (&jw )
if jw .Error != nil {
return false , 0 , jw .Error
}
w .Header ().Set ("Content-Type" , "application/json" )
w .Header ().Set ("Content-Length" , strconv .Itoa (jw .Size ()))
started = true
written , err = jw .DumpTo (w )
return
}
func Unmarshal (data []byte , v Unmarshaler ) error {
l := jlexer .Lexer {Data : data }
v .UnmarshalEasyJSON (&l )
return l .Error ()
}
func UnmarshalFromReader (r io .Reader , v Unmarshaler ) error {
data , err := ioutil .ReadAll (r )
if err != nil {
return err
}
l := jlexer .Lexer {Data : data }
v .UnmarshalEasyJSON (&l )
return l .Error ()
}
The pages are generated with Golds v0.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 .