package decoder
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
"unsafe"
"github.com/goccy/go-json/internal/jsonnum"
)
var (
errStringOptionRest = errors .New ("json: value followed by more bytes in a string of the string option" )
errMapKeyType = errors .New ("json: map key of another type" )
)
type wrappedStringDecoder struct {
typ reflect .Type
dec Decoder
stringDecoder *stringDecoder
structName string
fieldName string
isPtrType bool
isMapKey bool
numberKind reflect .Kind
numberType reflect .Type
isJSONNumber bool
}
func newWrappedStringDecoder(typ reflect .Type , dec Decoder , structName , fieldName string ) *wrappedStringDecoder {
d := &wrappedStringDecoder {
typ : typ ,
dec : dec ,
stringDecoder : newStringDecoder (structName , fieldName ),
structName : structName ,
fieldName : fieldName ,
isPtrType : typ .Kind () == reflect .Ptr ,
}
numberType := typ
if numberType .Kind () == reflect .Pointer {
numberType = numberType .Elem ()
}
d .isJSONNumber = numberType == jsonNumberType
switch numberType .Kind () {
case reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 ,
reflect .Uint , reflect .Uint8 , reflect .Uint16 , reflect .Uint32 , reflect .Uint64 , reflect .Uintptr ,
reflect .Float32 , reflect .Float64 :
d .numberKind , d .numberType = numberType .Kind (), numberType
}
return d
}
func (d *wrappedStringDecoder ) Decode (ctx *RuntimeContext , cursor , depth int64 , p unsafe .Pointer ) (int64 , error ) {
buf := ctx .Buf
cursor = skipWhiteSpace (buf , cursor )
start := cursor
if c := buf [cursor ]; c != 'n' && isOtherValue (c , stringValue ) {
if d .isPtrType {
d .allocate (p )
}
return ctx .stringOptionUnquoted (cursor , depth , d .typ )
}
bytes , c , err := d .stringDecoder .decodeByte (buf , cursor )
if err != nil {
return 0 , err
}
if bytes == nil {
if d .isPtrType {
*(*unsafe .Pointer )(p ) = nil
}
return c , nil
}
if d .isJSONNumber && !d .isMapKey {
if jsonnum .IsValid (bytes ) {
*(*json .Number )(d .target (p )) = json .Number (ctx .makeString (bytes ))
return c , nil
}
return d .stringError (ctx , start , c , bytes , p )
}
if d .numberKind != reflect .Invalid && len (bytes ) > 0 {
if d .decodeNumber (bytes , p ) {
return c , nil
}
if !d .isMapKey && string (bytes ) == "null" {
if d .isPtrType {
*(*unsafe .Pointer )(p ) = nil
}
return c , nil
}
return d .stringError (ctx , start , c , bytes , p )
}
if len (bytes ) != 0 && (isWhiteSpace [bytes [0 ]] || isWhiteSpace [bytes [len (bytes )-1 ]]) {
return d .stringError (ctx , start , c , bytes , p )
}
saved := ctx .typeError
ctx .typeError = nil
oldBuf , oldOrigin := ctx .Buf , ctx .origin
ctx .Buf = NewInput (bytes )
ctx .origin = ctx .Buf
next , err := d .dec .Decode (ctx , 0 , depth , p )
if err == nil && skipWhiteSpace (ctx .Buf , next ) != int64 (len (bytes )) {
err = errStringOptionRest
}
ctx .Buf , ctx .origin = oldBuf , oldOrigin
failed := err != nil || ctx .typeError != nil
ctx .typeError = saved
if failed || len (bytes ) == 0 {
return d .stringError (ctx , start , c , bytes , p )
}
return c , nil
}
func (d *wrappedStringDecoder ) stringError (ctx *RuntimeContext , start , end int64 , bytes []byte , p unsafe .Pointer ) (int64 , error ) {
if d .isMapKey {
ctx .keyTypeError (start , end , bytes , d .elemType ())
return end , errMapKeyType
}
if d .isPtrType {
d .allocate (p )
}
return ctx .stringOptionError (start , end , bytes , d .typ )
}
func (d *wrappedStringDecoder ) decodeNumber (bytes []byte , p unsafe .Pointer ) bool {
s := *(*string )(unsafe .Pointer (&bytes ))
bits := int (d .numberType .Size () * 8 )
switch d .numberKind {
case reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 :
n , err := strconv .ParseInt (s , 10 , bits )
if err != nil {
return false
}
storeInt (d .target (p ), bits , uint64 (n ))
case reflect .Uint , reflect .Uint8 , reflect .Uint16 , reflect .Uint32 , reflect .Uint64 , reflect .Uintptr :
n , err := strconv .ParseUint (s , 10 , bits )
if err != nil {
return false
}
storeInt (d .target (p ), bits , n )
default :
n , err := strconv .ParseFloat (s , bits )
if err != nil {
return false
}
if bits == 32 {
*(*float32 )(d .target (p )) = float32 (n )
} else {
*(*float64 )(d .target (p )) = n
}
}
return true
}
func storeInt(p unsafe .Pointer , bits int , n uint64 ) {
switch bits {
case 8 :
*(*uint8 )(p ) = uint8 (n )
case 16 :
*(*uint16 )(p ) = uint16 (n )
case 32 :
*(*uint32 )(p ) = uint32 (n )
default :
*(*uint64 )(p ) = n
}
}
func (d *wrappedStringDecoder ) target (p unsafe .Pointer ) unsafe .Pointer {
if !d .isPtrType {
return p
}
d .allocate (p )
return *(*unsafe .Pointer )(p )
}
func (d *wrappedStringDecoder ) elemType () reflect .Type {
typ := d .typ
for typ .Kind () == reflect .Pointer {
typ = typ .Elem ()
}
return typ
}
func (d *wrappedStringDecoder ) allocate (p unsafe .Pointer ) {
if *(*unsafe .Pointer )(p ) == nil {
*(*unsafe .Pointer )(p ) = newValue (d .typ .Elem ())
}
}
func (d *wrappedStringDecoder ) DecodePath (ctx *RuntimeContext , cursor , depth int64 ) ([][]byte , int64 , error ) {
return nil , 0 , fmt .Errorf ("json: wrapped string 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 .