package decoder
import (
"context"
"encoding/json"
"fmt"
"reflect"
"time"
"unsafe"
"github.com/goccy/go-json/internal/runtime"
)
type unmarshalJSONDecoder struct {
typ reflect .Type
retainsNothing bool
}
var timePtrType = reflect .TypeOf (&time .Time {})
var rawMessagePtrType = reflect .TypeOf (&json .RawMessage {})
var timeType = timePtrType .Elem ()
func newUnmarshalJSONDecoder(typ reflect .Type ) *unmarshalJSONDecoder {
return &unmarshalJSONDecoder {
typ : typ ,
retainsNothing : typ == rawMessagePtrType || runtime .IsStdMarshalerType (typ ) || promotesStdUnmarshalJSON (typ ),
}
}
func promotesStdUnmarshalJSON(typ reflect .Type ) bool {
_ , _ , _ , ok := runtime .PromotedStdMethod (typ , runtime .UnmarshalJSONMethod )
return ok
}
func (d *unmarshalJSONDecoder ) Decode (ctx *RuntimeContext , cursor , depth int64 , p unsafe .Pointer ) (int64 , error ) {
buf := ctx .Buf
cursor = skipWhiteSpace (buf , cursor )
if d .typ == timePtrType {
if c := buf [cursor ]; c != '"' && c != 'n' {
return ctx .skipTypeError (cursor , depth , timeType )
}
}
start := cursor
end , err := skipValue (buf , cursor , depth )
if err != nil {
return 0 , err
}
dst := buf [start :end :end ]
if !d .retainsNothing {
copied := make ([]byte , len (dst ))
copy (copied , dst )
dst = copied
}
v := *(*any )(unsafe .Pointer (&emptyInterface {
typ : runtime .TypePtr (d .typ ),
ptr : p ,
}))
switch v := v .(type ) {
case unmarshalerContext :
c := ctx .Option .Context
if (ctx .Option .Flags &ContextOption ) == 0 || c == nil {
c = context .Background ()
}
if err := v .UnmarshalJSON (c , dst ); err != nil {
ctx .methodError (err )
}
case json .Unmarshaler :
if err := v .UnmarshalJSON (dst ); err != nil {
ctx .methodError (err )
}
}
return end , nil
}
func (d *unmarshalJSONDecoder ) DecodePath (ctx *RuntimeContext , cursor , depth int64 ) ([][]byte , int64 , error ) {
return nil , 0 , fmt .Errorf ("json: unmarshal json decoder does not support decode path" )
}
The pages are generated with Golds v0.8.7 . (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 .