package decoder
import (
"bytes"
"fmt"
"reflect"
"sync"
"unsafe"
"github.com/goccy/go-json/internal/errors"
"github.com/goccy/go-json/internal/options"
"github.com/goccy/go-json/internal/runtime"
"github.com/goccy/go-json/jsontext"
)
var unmarshalerFromType = reflect .TypeFor [unmarshalerFrom ]()
type unmarshalerFrom interface {
UnmarshalJSONFrom(*jsontext .Decoder ) error
}
func implementsUnmarshalFrom(typ reflect .Type ) bool {
return typ != reflect .PointerTo (jsonNumberType ) &&
(typ .Implements (unmarshalerFromType ) || stdUnmarshalerFromType != nil && typ .Implements (stdUnmarshalerFromType ))
}
type fromDecoder struct {
r bytes .Reader
dec *jsontext .Decoder
}
var fromDecoders = sync .Pool {New : func () any {
d := new (fromDecoder )
d .dec = jsontext .NewDecoder (&d .r )
return d
}}
type fromOptions OptionFlags
func (o fromOptions ) ApplyTo (c *options .Config ) {
f := options .DefaultV1Flags
if OptionFlags (o )&DisallowUnknownFieldsOption != 0 {
f |= options .RejectUnknownMembers
}
if OptionFlags (o )&UseNumberOption != 0 {
f |= options .UnmarshalAnyWithRawNumber
}
c .Set |= f
c .Value |= f
}
func unmarshalFrom(m unmarshalerFrom , value []byte , flags OptionFlags ) (bool , error ) {
d := fromDecoders .Get ().(*fromDecoder )
defer fromDecoders .Put (d )
d .r .Reset (value )
d .dec .Reset (&d .r , fromOptions (flags ))
err := m .UnmarshalJSONFrom (d .dec )
_ , n := d .dec .StackIndex (0 )
declined , err := errors .MethodCallResult (err , d .dec .StackDepth (), n )
d .r .Reset (nil )
return declined , err
}
type unmarshalFromDecoder struct {
typ reflect .Type
from func (v any , value []byte , flags OptionFlags ) (bool , error )
next Decoder
}
func newUnmarshalFromDecoder(typ reflect .Type , next Decoder ) *unmarshalFromDecoder {
from := stdUnmarshalFrom
if typ .Implements (unmarshalerFromType ) {
from = func (v any , value []byte , flags OptionFlags ) (bool , error ) {
return unmarshalFrom (v .(unmarshalerFrom ), value , flags )
}
}
return &unmarshalFromDecoder {typ : typ , from : from , next : next }
}
func (d *unmarshalFromDecoder ) Decode (ctx *RuntimeContext , cursor , depth int64 , p unsafe .Pointer ) (int64 , error ) {
buf := ctx .Buf
start := skipWhiteSpace (buf , cursor )
end , err := skipValue (buf , start , depth )
if err != nil {
return 0 , err
}
v := *(*any )(unsafe .Pointer (&emptyInterface {
typ : runtime .TypePtr (d .typ ),
ptr : p ,
}))
declined , err := d .from (v , buf [start :end ], ctx .Option .Flags )
if declined {
return d .next .Decode (ctx , start , depth , p )
}
if err != nil {
ctx .methodError (err )
}
return end , nil
}
func (d *unmarshalFromDecoder ) DecodePath (ctx *RuntimeContext , cursor , depth int64 ) ([][]byte , int64 , error ) {
return nil , 0 , fmt .Errorf ("json: unmarshal json from 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 .