go-json/internal/decoder/struct.go

846 lines
20 KiB
Go
Raw Permalink Normal View History

2021-06-03 12:49:01 +03:00
package decoder
2020-04-23 19:39:20 +03:00
import (
2020-08-11 13:05:20 +03:00
"fmt"
"math"
"math/bits"
"sort"
"strings"
"unicode"
"unicode/utf16"
2020-04-23 19:39:20 +03:00
"unsafe"
2021-06-03 12:49:01 +03:00
"github.com/goccy/go-json/internal/errors"
2020-04-23 19:39:20 +03:00
)
type structFieldSet struct {
2021-06-03 13:10:17 +03:00
dec Decoder
2020-12-22 15:55:59 +03:00
offset uintptr
isTaggedKey bool
2021-06-05 07:00:26 +03:00
fieldIdx int
key string
keyLen int64
err error
2020-04-23 19:39:20 +03:00
}
type structDecoder struct {
2021-06-05 07:00:26 +03:00
fieldMap map[string]*structFieldSet
fieldUniqueNameNum int
stringDecoder *stringDecoder
structName string
fieldName string
isTriedOptimize bool
keyBitmapUint8 [][256]uint8
keyBitmapUint16 [][256]uint16
sortedFieldSets []*structFieldSet
keyDecoder func(*structDecoder, []byte, int64) (int64, *structFieldSet, error)
keyStreamDecoder func(*structDecoder, *Stream) (*structFieldSet, string, error)
}
var (
largeToSmallTable [256]byte
)
func init() {
for i := 0; i < 256; i++ {
c := i
if 'A' <= c && c <= 'Z' {
c += 'a' - 'A'
}
largeToSmallTable[i] = byte(c)
}
2020-04-23 19:39:20 +03:00
}
func toASCIILower(s string) string {
b := []byte(s)
for i := range b {
b[i] = largeToSmallTable[b[i]]
}
return string(b)
}
func newStructDecoder(structName, fieldName string, fieldMap map[string]*structFieldSet) *structDecoder {
2020-04-23 19:39:20 +03:00
return &structDecoder{
fieldMap: fieldMap,
stringDecoder: newStringDecoder(structName, fieldName),
structName: structName,
fieldName: fieldName,
keyDecoder: decodeKey,
keyStreamDecoder: decodeKeyStream,
2020-04-23 19:39:20 +03:00
}
}
const (
allowOptimizeMaxKeyLen = 64
allowOptimizeMaxFieldLen = 16
)
func (d *structDecoder) tryOptimize() {
2021-06-05 07:00:26 +03:00
fieldUniqueNameMap := map[string]int{}
fieldIdx := -1
for k, v := range d.fieldMap {
lower := strings.ToLower(k)
idx, exists := fieldUniqueNameMap[lower]
if exists {
v.fieldIdx = idx
} else {
fieldIdx++
v.fieldIdx = fieldIdx
}
fieldUniqueNameMap[lower] = fieldIdx
}
d.fieldUniqueNameNum = len(fieldUniqueNameMap)
if d.isTriedOptimize {
return
}
fieldMap := map[string]*structFieldSet{}
conflicted := map[string]struct{}{}
for k, v := range d.fieldMap {
key := strings.ToLower(k)
if key != k {
if key != toASCIILower(k) {
d.isTriedOptimize = true
return
}
// already exists same key (e.g. Hello and HELLO has same lower case key
if _, exists := conflicted[key]; exists {
d.isTriedOptimize = true
return
}
conflicted[key] = struct{}{}
}
if field, exists := fieldMap[key]; exists {
if field != v {
d.isTriedOptimize = true
return
}
}
fieldMap[key] = v
}
if len(fieldMap) > allowOptimizeMaxFieldLen {
d.isTriedOptimize = true
return
}
var maxKeyLen int
sortedKeys := []string{}
for key := range fieldMap {
keyLen := len(key)
if keyLen > allowOptimizeMaxKeyLen {
d.isTriedOptimize = true
return
}
if maxKeyLen < keyLen {
maxKeyLen = keyLen
}
sortedKeys = append(sortedKeys, key)
}
sort.Strings(sortedKeys)
2021-02-11 16:10:14 +03:00
// By allocating one extra capacity than `maxKeyLen`,
// it is possible to avoid the process of comparing the index of the key with the length of the bitmap each time.
bitmapLen := maxKeyLen + 1
2021-02-05 14:47:49 +03:00
if len(sortedKeys) <= 8 {
keyBitmap := make([][256]uint8, bitmapLen)
2021-02-05 14:47:49 +03:00
for i, key := range sortedKeys {
for j := 0; j < len(key); j++ {
c := key[j]
keyBitmap[j][c] |= (1 << uint(i))
}
d.sortedFieldSets = append(d.sortedFieldSets, fieldMap[key])
}
d.keyBitmapUint8 = keyBitmap
d.keyDecoder = decodeKeyByBitmapUint8
d.keyStreamDecoder = decodeKeyByBitmapUint8Stream
2021-02-05 14:47:49 +03:00
} else {
keyBitmap := make([][256]uint16, bitmapLen)
2021-02-05 14:47:49 +03:00
for i, key := range sortedKeys {
for j := 0; j < len(key); j++ {
c := key[j]
keyBitmap[j][c] |= (1 << uint(i))
}
d.sortedFieldSets = append(d.sortedFieldSets, fieldMap[key])
}
d.keyBitmapUint16 = keyBitmap
d.keyDecoder = decodeKeyByBitmapUint16
d.keyStreamDecoder = decodeKeyByBitmapUint16Stream
}
}
// decode from '\uXXXX'
func decodeKeyCharByUnicodeRune(buf []byte, cursor int64) ([]byte, int64, error) {
const defaultOffset = 4
const surrogateOffset = 6
if cursor+defaultOffset >= int64(len(buf)) {
return nil, 0, errors.ErrUnexpectedEndOfJSON("escaped string", cursor)
}
r := unicodeToRune(buf[cursor : cursor+defaultOffset])
if utf16.IsSurrogate(r) {
cursor += defaultOffset
if cursor+surrogateOffset >= int64(len(buf)) || buf[cursor] != '\\' || buf[cursor+1] != 'u' {
return []byte(string(unicode.ReplacementChar)), cursor + defaultOffset - 1, nil
}
cursor += 2
r2 := unicodeToRune(buf[cursor : cursor+defaultOffset])
if r := utf16.DecodeRune(r, r2); r != unicode.ReplacementChar {
return []byte(string(r)), cursor + defaultOffset - 1, nil
}
}
return []byte(string(r)), cursor + defaultOffset - 1, nil
}
func decodeKeyCharByEscapedChar(buf []byte, cursor int64) ([]byte, int64, error) {
c := buf[cursor]
cursor++
switch c {
case '"':
return []byte{'"'}, cursor, nil
case '\\':
return []byte{'\\'}, cursor, nil
case '/':
return []byte{'/'}, cursor, nil
case 'b':
return []byte{'\b'}, cursor, nil
case 'f':
return []byte{'\f'}, cursor, nil
case 'n':
return []byte{'\n'}, cursor, nil
case 'r':
return []byte{'\r'}, cursor, nil
case 't':
return []byte{'\t'}, cursor, nil
case 'u':
return decodeKeyCharByUnicodeRune(buf, cursor)
}
return nil, cursor, nil
}
func decodeKeyByBitmapUint8(d *structDecoder, buf []byte, cursor int64) (int64, *structFieldSet, error) {
var (
curBit uint8 = math.MaxUint8
)
b := (*sliceHeader)(unsafe.Pointer(&buf)).data
for {
switch char(b, cursor) {
case ' ', '\n', '\t', '\r':
cursor++
case '"':
cursor++
c := char(b, cursor)
switch c {
case '"':
cursor++
return cursor, nil, nil
case nul:
2021-06-03 12:49:01 +03:00
return 0, nil, errors.ErrUnexpectedEndOfJSON("string", cursor)
}
2021-02-05 14:47:49 +03:00
keyIdx := 0
bitmap := d.keyBitmapUint8
start := cursor
2021-02-05 14:47:49 +03:00
for {
c := char(b, cursor)
2021-02-05 14:47:49 +03:00
switch c {
case '"':
fieldSetIndex := bits.TrailingZeros8(curBit)
field := d.sortedFieldSets[fieldSetIndex]
keyLen := cursor - start
cursor++
if keyLen < field.keyLen {
// early match
return cursor, nil, nil
}
2021-02-05 14:47:49 +03:00
return cursor, field, nil
case nul:
2021-06-03 12:49:01 +03:00
return 0, nil, errors.ErrUnexpectedEndOfJSON("string", cursor)
case '\\':
cursor++
chars, nextCursor, err := decodeKeyCharByEscapedChar(buf, cursor)
if err != nil {
return 0, nil, err
}
for _, c := range chars {
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
if curBit == 0 {
return decodeKeyNotFound(b, cursor)
}
keyIdx++
}
cursor = nextCursor
2021-02-05 14:47:49 +03:00
default:
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
if curBit == 0 {
return decodeKeyNotFound(b, cursor)
2021-02-05 14:47:49 +03:00
}
keyIdx++
}
2021-02-05 14:47:49 +03:00
cursor++
}
default:
2021-08-13 11:05:07 +03:00
return cursor, nil, errors.ErrInvalidBeginningOfValue(char(b, cursor), cursor)
2021-02-05 14:47:49 +03:00
}
}
}
func decodeKeyByBitmapUint16(d *structDecoder, buf []byte, cursor int64) (int64, *structFieldSet, error) {
2021-02-05 14:47:49 +03:00
var (
curBit uint16 = math.MaxUint16
2021-02-05 14:47:49 +03:00
)
b := (*sliceHeader)(unsafe.Pointer(&buf)).data
2021-02-05 14:47:49 +03:00
for {
switch char(b, cursor) {
2021-02-05 14:47:49 +03:00
case ' ', '\n', '\t', '\r':
cursor++
case '"':
cursor++
c := char(b, cursor)
2021-02-05 14:47:49 +03:00
switch c {
case '"':
cursor++
return cursor, nil, nil
2021-02-05 14:47:49 +03:00
case nul:
2021-06-03 12:49:01 +03:00
return 0, nil, errors.ErrUnexpectedEndOfJSON("string", cursor)
}
2021-02-05 14:47:49 +03:00
keyIdx := 0
bitmap := d.keyBitmapUint16
start := cursor
for {
c := char(b, cursor)
switch c {
case '"':
fieldSetIndex := bits.TrailingZeros16(curBit)
field := d.sortedFieldSets[fieldSetIndex]
keyLen := cursor - start
cursor++
if keyLen < field.keyLen {
// early match
return cursor, nil, nil
}
2021-02-05 14:47:49 +03:00
return cursor, field, nil
case nul:
2021-06-03 12:49:01 +03:00
return 0, nil, errors.ErrUnexpectedEndOfJSON("string", cursor)
case '\\':
cursor++
chars, nextCursor, err := decodeKeyCharByEscapedChar(buf, cursor)
if err != nil {
return 0, nil, err
}
for _, c := range chars {
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
if curBit == 0 {
return decodeKeyNotFound(b, cursor)
}
keyIdx++
}
cursor = nextCursor
default:
2021-02-05 14:47:49 +03:00
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
if curBit == 0 {
return decodeKeyNotFound(b, cursor)
}
keyIdx++
}
cursor++
}
default:
2021-08-13 11:05:07 +03:00
return cursor, nil, errors.ErrInvalidBeginningOfValue(char(b, cursor), cursor)
}
}
2021-02-05 14:47:49 +03:00
}
func decodeKeyNotFound(b unsafe.Pointer, cursor int64) (int64, *structFieldSet, error) {
for {
cursor++
switch char(b, cursor) {
case '"':
cursor++
return cursor, nil, nil
case '\\':
cursor++
if char(b, cursor) == nul {
return 0, nil, errors.ErrUnexpectedEndOfJSON("string", cursor)
}
case nul:
return 0, nil, errors.ErrUnexpectedEndOfJSON("string", cursor)
}
}
}
2021-02-05 14:47:49 +03:00
func decodeKey(d *structDecoder, buf []byte, cursor int64) (int64, *structFieldSet, error) {
key, c, err := d.stringDecoder.decodeByte(buf, cursor)
if err != nil {
return 0, nil, err
}
2021-02-05 14:47:49 +03:00
cursor = c
k := *(*string)(unsafe.Pointer(&key))
field, exists := d.fieldMap[k]
if !exists {
return cursor, nil, nil
}
return cursor, field, nil
}
2021-06-03 12:49:01 +03:00
func decodeKeyByBitmapUint8Stream(d *structDecoder, s *Stream) (*structFieldSet, string, error) {
var (
curBit uint8 = math.MaxUint8
)
_, cursor, p := s.stat()
for {
2021-02-15 20:13:13 +03:00
switch char(p, cursor) {
case ' ', '\n', '\t', '\r':
2021-02-15 20:13:13 +03:00
cursor++
2021-02-06 15:42:19 +03:00
case nul:
2021-02-15 20:13:13 +03:00
s.cursor = cursor
2021-02-06 15:42:19 +03:00
if s.read() {
_, cursor, p = s.stat()
2021-02-06 15:42:19 +03:00
continue
}
2021-08-13 11:05:07 +03:00
return nil, "", errors.ErrInvalidBeginningOfValue(char(p, cursor), s.totalOffset())
case '"':
2021-02-15 20:13:13 +03:00
cursor++
FIRST_CHAR:
2021-02-15 20:13:13 +03:00
start := cursor
switch char(p, cursor) {
case '"':
2021-02-15 20:13:13 +03:00
cursor++
s.cursor = cursor
return nil, "", nil
case nul:
2021-02-15 20:13:13 +03:00
s.cursor = cursor
if s.read() {
_, cursor, p = s.stat()
goto FIRST_CHAR
}
2021-06-03 12:49:01 +03:00
return nil, "", errors.ErrUnexpectedEndOfJSON("string", s.totalOffset())
}
keyIdx := 0
bitmap := d.keyBitmapUint8
for {
2021-02-15 20:13:13 +03:00
c := char(p, cursor)
switch c {
case '"':
fieldSetIndex := bits.TrailingZeros8(curBit)
field := d.sortedFieldSets[fieldSetIndex]
2021-02-15 20:13:13 +03:00
keyLen := cursor - start
cursor++
s.cursor = cursor
if keyLen < field.keyLen {
// early match
return nil, field.key, nil
}
return field, field.key, nil
case nul:
2021-02-15 20:13:13 +03:00
s.cursor = cursor
if s.read() {
_, cursor, p = s.stat()
continue
}
2021-06-03 12:49:01 +03:00
return nil, "", errors.ErrUnexpectedEndOfJSON("string", s.totalOffset())
case '\\':
s.cursor = cursor + 1 // skip '\' char
chars, err := decodeKeyCharByEscapeCharStream(s)
if err != nil {
return nil, "", err
}
cursor = s.cursor
for _, c := range chars {
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
if curBit == 0 {
s.cursor = cursor
return decodeKeyNotFoundStream(s, start)
}
keyIdx++
}
default:
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
if curBit == 0 {
s.cursor = cursor
return decodeKeyNotFoundStream(s, start)
}
keyIdx++
}
2021-02-15 20:13:13 +03:00
cursor++
}
default:
2021-08-13 11:05:07 +03:00
return nil, "", errors.ErrInvalidBeginningOfValue(char(p, cursor), s.totalOffset())
}
}
}
2021-06-03 12:49:01 +03:00
func decodeKeyByBitmapUint16Stream(d *structDecoder, s *Stream) (*structFieldSet, string, error) {
var (
curBit uint16 = math.MaxUint16
)
_, cursor, p := s.stat()
for {
2021-02-15 20:13:13 +03:00
switch char(p, cursor) {
case ' ', '\n', '\t', '\r':
2021-02-15 20:13:13 +03:00
cursor++
2021-02-06 15:42:19 +03:00
case nul:
2021-02-15 20:13:13 +03:00
s.cursor = cursor
2021-02-06 15:42:19 +03:00
if s.read() {
_, cursor, p = s.stat()
2021-02-06 15:42:19 +03:00
continue
}
2021-08-13 11:05:07 +03:00
return nil, "", errors.ErrInvalidBeginningOfValue(char(p, cursor), s.totalOffset())
case '"':
2021-02-15 20:13:13 +03:00
cursor++
FIRST_CHAR:
2021-02-15 20:13:13 +03:00
start := cursor
switch char(p, cursor) {
case '"':
2021-02-15 20:13:13 +03:00
cursor++
s.cursor = cursor
return nil, "", nil
case nul:
2021-02-15 20:13:13 +03:00
s.cursor = cursor
if s.read() {
_, cursor, p = s.stat()
goto FIRST_CHAR
}
2021-06-03 12:49:01 +03:00
return nil, "", errors.ErrUnexpectedEndOfJSON("string", s.totalOffset())
}
keyIdx := 0
bitmap := d.keyBitmapUint16
for {
2021-02-15 20:13:13 +03:00
c := char(p, cursor)
switch c {
case '"':
fieldSetIndex := bits.TrailingZeros16(curBit)
field := d.sortedFieldSets[fieldSetIndex]
2021-02-15 20:13:13 +03:00
keyLen := cursor - start
cursor++
s.cursor = cursor
if keyLen < field.keyLen {
// early match
return nil, field.key, nil
}
return field, field.key, nil
case nul:
2021-02-15 20:13:13 +03:00
s.cursor = cursor
if s.read() {
_, cursor, p = s.stat()
continue
}
2021-06-03 12:49:01 +03:00
return nil, "", errors.ErrUnexpectedEndOfJSON("string", s.totalOffset())
case '\\':
s.cursor = cursor + 1 // skip '\' char
chars, err := decodeKeyCharByEscapeCharStream(s)
if err != nil {
return nil, "", err
}
cursor = s.cursor
for _, c := range chars {
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
if curBit == 0 {
s.cursor = cursor
return decodeKeyNotFoundStream(s, start)
}
keyIdx++
}
default:
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
if curBit == 0 {
s.cursor = cursor
return decodeKeyNotFoundStream(s, start)
}
keyIdx++
}
2021-02-15 20:13:13 +03:00
cursor++
}
default:
2021-08-13 11:05:07 +03:00
return nil, "", errors.ErrInvalidBeginningOfValue(char(p, cursor), s.totalOffset())
}
}
}
// decode from '\uXXXX'
func decodeKeyCharByUnicodeRuneStream(s *Stream) ([]byte, error) {
const defaultOffset = 4
const surrogateOffset = 6
if s.cursor+defaultOffset >= s.length {
if !s.read() {
return nil, errors.ErrInvalidCharacter(s.char(), "escaped unicode char", s.totalOffset())
}
}
r := unicodeToRune(s.buf[s.cursor : s.cursor+defaultOffset])
if utf16.IsSurrogate(r) {
s.cursor += defaultOffset
if s.cursor+surrogateOffset >= s.length {
s.read()
}
if s.cursor+surrogateOffset >= s.length || s.buf[s.cursor] != '\\' || s.buf[s.cursor+1] != 'u' {
s.cursor += defaultOffset - 1
return []byte(string(unicode.ReplacementChar)), nil
}
r2 := unicodeToRune(s.buf[s.cursor+defaultOffset+2 : s.cursor+surrogateOffset])
if r := utf16.DecodeRune(r, r2); r != unicode.ReplacementChar {
s.cursor += defaultOffset - 1
return []byte(string(r)), nil
}
}
s.cursor += defaultOffset - 1
return []byte(string(r)), nil
}
func decodeKeyCharByEscapeCharStream(s *Stream) ([]byte, error) {
c := s.buf[s.cursor]
s.cursor++
RETRY:
switch c {
case '"':
return []byte{'"'}, nil
case '\\':
return []byte{'\\'}, nil
case '/':
return []byte{'/'}, nil
case 'b':
return []byte{'\b'}, nil
case 'f':
return []byte{'\f'}, nil
case 'n':
return []byte{'\n'}, nil
case 'r':
return []byte{'\r'}, nil
case 't':
return []byte{'\t'}, nil
case 'u':
return decodeKeyCharByUnicodeRuneStream(s)
case nul:
if !s.read() {
return nil, errors.ErrInvalidCharacter(s.char(), "escaped char", s.totalOffset())
}
goto RETRY
default:
return nil, errors.ErrUnexpectedEndOfJSON("struct field", s.totalOffset())
}
}
func decodeKeyNotFoundStream(s *Stream, start int64) (*structFieldSet, string, error) {
buf, cursor, p := s.stat()
for {
cursor++
switch char(p, cursor) {
case '"':
b := buf[start:cursor]
key := *(*string)(unsafe.Pointer(&b))
cursor++
s.cursor = cursor
return nil, key, nil
case '\\':
cursor++
if char(p, cursor) == nul {
s.cursor = cursor
if !s.read() {
return nil, "", errors.ErrUnexpectedEndOfJSON("string", s.totalOffset())
}
buf, cursor, p = s.statForRetry()
}
case nul:
s.cursor = cursor
if !s.read() {
return nil, "", errors.ErrUnexpectedEndOfJSON("string", s.totalOffset())
}
buf, cursor, p = s.statForRetry()
}
}
}
2021-06-03 12:49:01 +03:00
func decodeKeyStream(d *structDecoder, s *Stream) (*structFieldSet, string, error) {
key, err := d.stringDecoder.decodeStreamByte(s)
if err != nil {
return nil, "", err
}
k := *(*string)(unsafe.Pointer(&key))
return d.fieldMap[k], k, nil
}
2021-06-03 12:49:01 +03:00
func (d *structDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error {
depth++
if depth > maxDecodeNestingDepth {
2021-06-03 12:49:01 +03:00
return errors.ErrExceededMaxDepth(s.char(), s.cursor)
}
2021-06-04 08:59:43 +03:00
c := s.skipWhiteSpace()
switch c {
case 'n':
if err := nullBytes(s); err != nil {
return err
}
return nil
default:
if s.char() != '{' {
2021-08-13 11:05:07 +03:00
return errors.ErrInvalidBeginningOfValue(s.char(), s.totalOffset())
}
2020-07-30 16:41:53 +03:00
}
2020-07-31 11:10:03 +03:00
s.cursor++
2021-06-04 08:59:43 +03:00
if s.skipWhiteSpace() == '}' {
2020-12-24 11:47:53 +03:00
s.cursor++
return nil
}
var (
seenFields map[int]struct{}
seenFieldNum int
)
firstWin := (s.Option.Flags & FirstWinOption) != 0
if firstWin {
seenFields = make(map[int]struct{}, d.fieldUniqueNameNum)
}
2020-07-30 16:41:53 +03:00
for {
s.reset()
field, key, err := d.keyStreamDecoder(d, s)
2020-07-30 16:41:53 +03:00
if err != nil {
return err
}
2021-06-04 08:59:43 +03:00
if s.skipWhiteSpace() != ':' {
2021-06-03 12:49:01 +03:00
return errors.ErrExpected("colon after object key", s.totalOffset())
2020-07-30 16:41:53 +03:00
}
2020-07-31 11:10:03 +03:00
s.cursor++
if field != nil {
if field.err != nil {
return field.err
}
if firstWin {
if _, exists := seenFields[field.fieldIdx]; exists {
if err := s.skipValue(depth); err != nil {
return err
}
} else {
if err := field.dec.DecodeStream(s, depth, unsafe.Pointer(uintptr(p)+field.offset)); err != nil {
return err
}
seenFieldNum++
if d.fieldUniqueNameNum <= seenFieldNum {
return s.skipObject(depth)
}
seenFields[field.fieldIdx] = struct{}{}
}
} else {
if err := field.dec.DecodeStream(s, depth, unsafe.Pointer(uintptr(p)+field.offset)); err != nil {
return err
}
2020-07-30 16:41:53 +03:00
}
2021-06-03 12:49:01 +03:00
} else if s.DisallowUnknownFields {
return fmt.Errorf("json: unknown field %q", key)
2020-07-30 16:41:53 +03:00
} else {
if err := s.skipValue(depth); err != nil {
2020-07-30 16:41:53 +03:00
return err
}
}
2021-06-04 08:59:43 +03:00
c := s.skipWhiteSpace()
2020-07-30 16:41:53 +03:00
if c == '}' {
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
return nil
}
if c != ',' {
2021-06-03 12:49:01 +03:00
return errors.ErrExpected("comma after object element", s.totalOffset())
2020-07-30 16:41:53 +03:00
}
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
}
}
2021-06-04 19:08:27 +03:00
func (d *structDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
buf := ctx.Buf
depth++
if depth > maxDecodeNestingDepth {
2021-06-03 12:49:01 +03:00
return 0, errors.ErrExceededMaxDepth(buf[cursor], cursor)
}
2020-05-23 06:51:09 +03:00
buflen := int64(len(buf))
2020-05-06 20:37:29 +03:00
cursor = skipWhiteSpace(buf, cursor)
2021-02-05 19:19:24 +03:00
b := (*sliceHeader)(unsafe.Pointer(&buf)).data
switch char(b, cursor) {
case 'n':
2021-05-08 21:05:36 +03:00
if err := validateNull(buf, cursor); err != nil {
return 0, err
}
cursor += 4
return cursor, nil
case '{':
default:
2021-08-13 11:05:07 +03:00
return 0, errors.ErrInvalidBeginningOfValue(char(b, cursor), cursor)
2020-05-23 06:51:09 +03:00
}
2020-04-23 19:39:20 +03:00
cursor++
2021-02-15 12:45:58 +03:00
cursor = skipWhiteSpace(buf, cursor)
if buf[cursor] == '}' {
cursor++
return cursor, nil
}
2021-06-05 07:00:26 +03:00
var (
seenFields map[int]struct{}
seenFieldNum int
)
firstWin := (ctx.Option.Flags & FirstWinOption) != 0
2021-06-05 07:00:26 +03:00
if firstWin {
seenFields = make(map[int]struct{}, d.fieldUniqueNameNum)
}
2021-02-05 19:19:24 +03:00
for {
2021-02-05 14:47:49 +03:00
c, field, err := d.keyDecoder(d, buf, cursor)
2020-04-23 19:39:20 +03:00
if err != nil {
2020-05-06 20:37:29 +03:00
return 0, err
2020-04-23 19:39:20 +03:00
}
2021-02-05 19:19:24 +03:00
cursor = skipWhiteSpace(buf, c)
if char(b, cursor) != ':' {
2021-06-03 12:49:01 +03:00
return 0, errors.ErrExpected("colon after object key", cursor)
2021-02-05 14:47:49 +03:00
}
cursor++
2021-02-05 19:19:24 +03:00
if cursor >= buflen {
2021-06-03 12:49:01 +03:00
return 0, errors.ErrExpected("object value after colon", cursor)
2021-02-05 14:47:49 +03:00
}
if field != nil {
if field.err != nil {
return 0, field.err
}
2021-06-05 07:00:26 +03:00
if firstWin {
if _, exists := seenFields[field.fieldIdx]; exists {
c, err := skipValue(buf, cursor, depth)
if err != nil {
return 0, err
}
cursor = c
} else {
c, err := field.dec.Decode(ctx, cursor, depth, unsafe.Pointer(uintptr(p)+field.offset))
if err != nil {
return 0, err
}
cursor = c
seenFieldNum++
if d.fieldUniqueNameNum <= seenFieldNum {
return skipObject(buf, cursor, depth)
}
seenFields[field.fieldIdx] = struct{}{}
}
} else {
c, err := field.dec.Decode(ctx, cursor, depth, unsafe.Pointer(uintptr(p)+field.offset))
if err != nil {
return 0, err
}
cursor = c
2020-04-23 19:39:20 +03:00
}
} else {
c, err := skipValue(buf, cursor, depth)
2020-05-06 20:37:29 +03:00
if err != nil {
return 0, err
2020-04-23 19:39:20 +03:00
}
2020-05-06 20:37:29 +03:00
cursor = c
2020-04-23 19:39:20 +03:00
}
2020-05-06 20:37:29 +03:00
cursor = skipWhiteSpace(buf, cursor)
2021-02-05 19:19:24 +03:00
if char(b, cursor) == '}' {
2020-05-06 20:37:29 +03:00
cursor++
return cursor, nil
2020-04-23 19:39:20 +03:00
}
2021-02-05 19:19:24 +03:00
if char(b, cursor) != ',' {
2021-06-03 12:49:01 +03:00
return 0, errors.ErrExpected("comma after object element", cursor)
2020-04-23 19:39:20 +03:00
}
2021-02-05 19:19:24 +03:00
cursor++
2020-04-23 19:39:20 +03:00
}
}
2022-11-28 21:55:56 +03:00
func (d *structDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) {
return nil, 0, fmt.Errorf("json: struct decoder does not support decode path")
}