package decoder
import (
"encoding"
"encoding/json"
"fmt"
"reflect"
"time"
"unsafe"
"github.com/goccy/go-json/internal/errors"
)
const (
noMethod = iota
methodUnmarshalJSONFrom
methodUnmarshalJSON
methodUnmarshalText
)
var V2Hooks struct {
UnmarshalerFromType reflect .Type
UnmarshalFrom func (ctx *RuntimeContext , cursor, depth int64 , typ reflect .Type , p unsafe .Pointer ) (int64 , error )
SkipStreamValue func (s *Stream , depth int64 ) error
Lend func (ctx *RuntimeContext )
AppendUnquote func (dst []byte , src string ) ([]byte , error )
}
func v2MethodOf(typ reflect .Type , first int ) int {
if typ .Kind () == reflect .Pointer || typ .Kind () == reflect .Interface {
return noMethod
}
ptr := reflect .PointerTo (typ )
switch {
case first <= methodUnmarshalJSONFrom && V2Hooks .UnmarshalerFromType != nil && ptr .Implements (V2Hooks .UnmarshalerFromType ):
return methodUnmarshalJSONFrom
case first <= methodUnmarshalJSON && ptr .Implements (unmarshalJSONType ):
return methodUnmarshalJSON
case ptr .Implements (unmarshalTextType ):
return methodUnmarshalText
}
return noMethod
}
type v2UnmarshalFromDecoder struct {
typ reflect .Type
next Decoder
}
func (d *v2UnmarshalFromDecoder ) Decode (ctx *RuntimeContext , cursor , depth int64 , p unsafe .Pointer ) (int64 , error ) {
copyStreamNames (ctx )
end , err := V2Hooks .UnmarshalFrom (ctx , cursor , depth , d .typ , p )
if err == ErrUseDefault {
return d .next .Decode (ctx , cursor , depth , p )
}
return end , err
}
func (d *v2UnmarshalFromDecoder ) DecodePath (ctx *RuntimeContext , cursor , depth int64 ) ([][]byte , int64 , error ) {
return nil , 0 , fmt .Errorf ("json: v2 decoder does not support decode path" )
}
func (ctx *RuntimeContext ) ReadValue (cursor , depth int64 , typ reflect .Type , method errors .UnmarshalMethod ) (int64 , int64 , error ) {
start := skipWhiteSpace (ctx .Buf , cursor )
end , err := ctx .skipValueV2 (start , depth )
if err != nil {
return 0 , 0 , &errors .UnmarshalError {GoType : typ , Start : start , Err : err , Method : method }
}
return start , end , nil
}
func readTextV2(ctx *RuntimeContext , cursor , depth int64 , typ reflect .Type , method errors .UnmarshalMethod ) ([]byte , int64 , int64 , error ) {
buf := ctx .Buf
if start := skipWhiteSpace (buf , cursor ); buf [start ] == '"' {
if q := plainStringEnd (buf , start +1 ); q >= 0 {
return buf [start +1 : q : q ], start , q + 1 , nil
}
}
start , end , err := ctx .ReadValue (cursor , depth , typ , method )
if err != nil || buf [start ] != '"' {
return nil , start , end , err
}
text := unquotedName (buf [start :end ])
return text [:len (text ):len (text )], start , end , nil
}
type v2UnmarshalJSONDecoder struct {
typ reflect .Type
ptrType unsafe .Pointer
}
func (d *v2UnmarshalJSONDecoder ) Decode (ctx *RuntimeContext , cursor , depth int64 , p unsafe .Pointer ) (int64 , error ) {
start , end , err := ctx .ReadValue (cursor , depth , d .typ , errors .UnmarshalJSONMethod )
if err != nil {
return 0 , err
}
v := *(*any )(unsafe .Pointer (&emptyInterface {typ : d .ptrType , ptr : p }))
if err := v .(json .Unmarshaler ).UnmarshalJSON (ctx .Buf [start :end :end ]); err != nil {
return 0 , &errors .UnmarshalError {GoType : d .typ , Start : start , Err : err , Method : errors .UnmarshalJSONMethod }
}
return end , nil
}
func (d *v2UnmarshalJSONDecoder ) DecodePath (ctx *RuntimeContext , cursor , depth int64 ) ([][]byte , int64 , error ) {
return nil , 0 , fmt .Errorf ("json: v2 decoder does not support decode path" )
}
var errNonStringValue = fmt .Errorf ("JSON value must be string type" )
type v2UnmarshalTextDecoder struct {
typ reflect .Type
ptrType unsafe .Pointer
zero zeroer
}
func (d *v2UnmarshalTextDecoder ) Decode (ctx *RuntimeContext , cursor , depth int64 , p unsafe .Pointer ) (int64 , error ) {
text , start , end , err := readTextV2 (ctx , cursor , depth , d .typ , errors .UnmarshalTextMethod )
if err != nil {
return 0 , err
}
switch ctx .Buf [start ] {
case 'n' :
if ctx .Option .V2 &V2MergeWithLegacySemantics == 0 {
d .zero .zero (p )
}
return end , nil
case '"' :
default :
return 0 , &errors .UnmarshalError {GoType : d .typ , Start : start , Err : errNonStringValue , Method : errors .UnmarshalTextMethod }
}
v := *(*any )(unsafe .Pointer (&emptyInterface {typ : d .ptrType , ptr : p }))
if err := v .(encoding .TextUnmarshaler ).UnmarshalText (text ); err != nil {
return 0 , &errors .UnmarshalError {GoType : d .typ , Start : start , Err : err , Method : errors .UnmarshalTextMethod }
}
return end , nil
}
func (d *v2UnmarshalTextDecoder ) DecodePath (ctx *RuntimeContext , cursor , depth int64 ) ([][]byte , int64 , error ) {
return nil , 0 , fmt .Errorf ("json: v2 decoder does not support decode path" )
}
type v2TimeDecoder struct {}
func (d v2TimeDecoder ) Decode (ctx *RuntimeContext , cursor , depth int64 , p unsafe .Pointer ) (int64 , error ) {
text , start , end , err := readTextV2 (ctx , cursor , depth , timeType , errors .UnmarshalTextMethod )
if err != nil {
return 0 , err
}
switch ctx .Buf [start ] {
case 'n' :
if ctx .Option .V2 &V2MergeWithLegacySemantics == 0 {
*(*time .Time )(p ) = time .Time {}
}
return end , nil
case '"' :
default :
return 0 , &errors .UnmarshalError {GoType : timeType , Start : start , Method : errors .UnmarshalTextMethod }
}
var t time .Time
if err := t .UnmarshalText (text ); err != nil {
return 0 , &errors .UnmarshalError {GoType : timeType , Start : start , Err : err , Method : errors .UnmarshalTextMethod }
}
if ctx .Option .V2 &V2ParseTimeWithLooseRFC3339 == 0 {
if err := strictRFC3339Error (text ); err != nil {
return 0 , &errors .UnmarshalError {GoType : timeType , Start : start , Err : err , Method : errors .UnmarshalTextMethod }
}
}
*(*time .Time )(p ) = t
return end , nil
}
func (d v2TimeDecoder ) DecodePath (ctx *RuntimeContext , cursor , depth int64 ) ([][]byte , int64 , error ) {
return nil , 0 , fmt .Errorf ("json: v2 decoder does not support decode path" )
}
func strictRFC3339Error(b []byte ) error {
parseError := func (valueElem , layoutElem , message string ) error {
return &time .ParseError {Layout : time .RFC3339 , Value : string (b ), LayoutElem : layoutElem , ValueElem : valueElem , Message : message }
}
switch {
case b [len ("2006-01-02T" )+1 ] == ':' :
return parseError (string (b [len ("2006-01-02T" ):][:1 ]), "15" , "" )
case b [len ("2006-01-02T15:04:05" )] == ',' :
return parseError ("," , "." , "" )
case b [len (b )-1 ] != 'Z' :
zone := b [len (b )-len ("Z07:00" ):]
switch {
case dec2 (zone [len ("Z" ):]) >= 24 :
return parseError (string (zone ), "Z07:00" , ": timezone hour out of range" )
case dec2 (zone [len ("Z07:" ):]) >= 60 :
return parseError (string (zone ), "Z07:00" , ": timezone minute out of range" )
}
}
return nil
}
func dec2(b []byte ) int {
if len (b ) < 2 || b [0 ]-'0' > 9 || b [1 ]-'0' > 9 {
return 100
}
return int (b [0 ]-'0' )*10 + int (b [1 ]-'0' )
}
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 .