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"
|
2021-02-04 14:58:58 +03:00
|
|
|
"math"
|
2021-02-15 14:05:35 +03:00
|
|
|
"math/bits"
|
2021-02-04 14:58:58 +03:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2021-06-02 11:23:32 +03:00
|
|
|
"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
|
2021-02-06 14:07:01 +03:00
|
|
|
key string
|
|
|
|
keyLen int64
|
2021-02-18 11:46:28 +03:00
|
|
|
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)
|
2021-02-04 14:58:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
func newStructDecoder(structName, fieldName string, fieldMap map[string]*structFieldSet) *structDecoder {
|
2020-04-23 19:39:20 +03:00
|
|
|
return &structDecoder{
|
2021-02-06 08:24:07 +03:00
|
|
|
fieldMap: fieldMap,
|
|
|
|
stringDecoder: newStringDecoder(structName, fieldName),
|
|
|
|
structName: structName,
|
|
|
|
fieldName: fieldName,
|
|
|
|
keyDecoder: decodeKey,
|
|
|
|
keyStreamDecoder: decodeKeyStream,
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 14:58:58 +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)
|
|
|
|
|
2021-02-04 14:58:58 +03:00
|
|
|
if d.isTriedOptimize {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fieldMap := map[string]*structFieldSet{}
|
2021-02-06 08:24:07 +03:00
|
|
|
conflicted := map[string]struct{}{}
|
2021-02-04 14:58:58 +03:00
|
|
|
for k, v := range d.fieldMap {
|
2021-02-06 08:24:07 +03:00
|
|
|
key := strings.ToLower(k)
|
|
|
|
if key != k {
|
|
|
|
// 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
|
2021-02-04 14:58:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-02-15 14:05:35 +03:00
|
|
|
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])
|
|
|
|
}
|
2021-02-15 14:05:35 +03:00
|
|
|
d.keyBitmapUint8 = keyBitmap
|
|
|
|
d.keyDecoder = decodeKeyByBitmapUint8
|
|
|
|
d.keyStreamDecoder = decodeKeyByBitmapUint8Stream
|
2021-02-05 14:47:49 +03:00
|
|
|
} else {
|
2021-02-15 14:05:35 +03:00
|
|
|
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])
|
2021-02-04 14:58:58 +03:00
|
|
|
}
|
2021-02-15 14:05:35 +03:00
|
|
|
d.keyBitmapUint16 = keyBitmap
|
|
|
|
d.keyDecoder = decodeKeyByBitmapUint16
|
|
|
|
d.keyStreamDecoder = decodeKeyByBitmapUint16Stream
|
2021-02-04 14:58:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 11:23:32 +03:00
|
|
|
// decode from '\uXXXX'
|
|
|
|
func decodeKeyCharByUnicodeRune(buf []byte, cursor int64) ([]byte, int64) {
|
|
|
|
const defaultOffset = 4
|
|
|
|
const surrogateOffset = 6
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
cursor += 2
|
|
|
|
r2 := unicodeToRune(buf[cursor : cursor+defaultOffset])
|
|
|
|
if r := utf16.DecodeRune(r, r2); r != unicode.ReplacementChar {
|
|
|
|
return []byte(string(r)), cursor + defaultOffset - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return []byte(string(r)), cursor + defaultOffset - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeKeyCharByEscapedChar(buf []byte, cursor int64) ([]byte, int64) {
|
|
|
|
c := buf[cursor]
|
|
|
|
cursor++
|
|
|
|
switch c {
|
|
|
|
case '"':
|
|
|
|
return []byte{'"'}, cursor
|
|
|
|
case '\\':
|
|
|
|
return []byte{'\\'}, cursor
|
|
|
|
case '/':
|
|
|
|
return []byte{'/'}, cursor
|
|
|
|
case 'b':
|
|
|
|
return []byte{'\b'}, cursor
|
|
|
|
case 'f':
|
|
|
|
return []byte{'\f'}, cursor
|
|
|
|
case 'n':
|
|
|
|
return []byte{'\n'}, cursor
|
|
|
|
case 'r':
|
|
|
|
return []byte{'\r'}, cursor
|
|
|
|
case 't':
|
|
|
|
return []byte{'\t'}, cursor
|
|
|
|
case 'u':
|
|
|
|
return decodeKeyCharByUnicodeRune(buf, cursor)
|
|
|
|
}
|
|
|
|
return nil, cursor
|
|
|
|
}
|
|
|
|
|
2021-02-15 14:05:35 +03:00
|
|
|
func decodeKeyByBitmapUint8(d *structDecoder, buf []byte, cursor int64) (int64, *structFieldSet, error) {
|
2021-02-04 14:58:58 +03:00
|
|
|
var (
|
2021-02-15 14:05:35 +03:00
|
|
|
curBit uint8 = math.MaxUint8
|
2021-02-04 14:58:58 +03:00
|
|
|
)
|
2021-02-05 19:09:24 +03:00
|
|
|
b := (*sliceHeader)(unsafe.Pointer(&buf)).data
|
2021-02-04 14:58:58 +03:00
|
|
|
for {
|
2021-02-05 19:09:24 +03:00
|
|
|
switch char(b, cursor) {
|
2021-02-04 14:58:58 +03:00
|
|
|
case ' ', '\n', '\t', '\r':
|
|
|
|
cursor++
|
|
|
|
case '"':
|
|
|
|
cursor++
|
2021-02-05 19:09:24 +03:00
|
|
|
c := char(b, cursor)
|
2021-02-04 14:58:58 +03:00
|
|
|
switch c {
|
|
|
|
case '"':
|
|
|
|
cursor++
|
2021-06-03 09:00:31 +03:00
|
|
|
return cursor, nil, nil
|
2021-02-04 14:58:58 +03:00
|
|
|
case nul:
|
2021-06-03 12:49:01 +03:00
|
|
|
return 0, nil, errors.ErrUnexpectedEndOfJSON("string", cursor)
|
2021-02-04 14:58:58 +03:00
|
|
|
}
|
2021-02-05 14:47:49 +03:00
|
|
|
keyIdx := 0
|
2021-02-15 14:05:35 +03:00
|
|
|
bitmap := d.keyBitmapUint8
|
2021-02-06 14:07:01 +03:00
|
|
|
start := cursor
|
2021-02-05 14:47:49 +03:00
|
|
|
for {
|
2021-02-05 19:09:24 +03:00
|
|
|
c := char(b, cursor)
|
2021-02-05 14:47:49 +03:00
|
|
|
switch c {
|
|
|
|
case '"':
|
2021-02-15 14:05:35 +03:00
|
|
|
fieldSetIndex := bits.TrailingZeros8(curBit)
|
2021-06-03 09:00:31 +03:00
|
|
|
field := d.sortedFieldSets[fieldSetIndex]
|
2021-02-06 14:07:01 +03:00
|
|
|
keyLen := cursor - start
|
2021-02-04 14:58:58 +03:00
|
|
|
cursor++
|
2021-02-06 14:07:01 +03:00
|
|
|
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)
|
2021-06-02 11:23:32 +03:00
|
|
|
case '\\':
|
|
|
|
cursor++
|
|
|
|
chars, nextCursor := decodeKeyCharByEscapedChar(buf, cursor)
|
|
|
|
for _, c := range chars {
|
|
|
|
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
|
|
|
|
if curBit == 0 {
|
2021-06-03 09:00:31 +03:00
|
|
|
return decodeKeyNotFound(b, cursor)
|
2021-06-02 11:23:32 +03:00
|
|
|
}
|
|
|
|
keyIdx++
|
|
|
|
}
|
|
|
|
cursor = nextCursor
|
2021-02-05 14:47:49 +03:00
|
|
|
default:
|
|
|
|
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
|
|
|
|
if curBit == 0 {
|
2021-06-03 09:00:31 +03:00
|
|
|
return decodeKeyNotFound(b, cursor)
|
2021-02-05 14:47:49 +03:00
|
|
|
}
|
|
|
|
keyIdx++
|
2021-02-04 14:58:58 +03:00
|
|
|
}
|
2021-02-05 14:47:49 +03:00
|
|
|
cursor++
|
|
|
|
}
|
|
|
|
default:
|
2021-06-03 12:49:01 +03:00
|
|
|
return cursor, nil, errors.ErrNotAtBeginningOfValue(cursor)
|
2021-02-05 14:47:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 14:05:35 +03:00
|
|
|
func decodeKeyByBitmapUint16(d *structDecoder, buf []byte, cursor int64) (int64, *structFieldSet, error) {
|
2021-02-05 14:47:49 +03:00
|
|
|
var (
|
2021-02-15 14:05:35 +03:00
|
|
|
curBit uint16 = math.MaxUint16
|
2021-02-05 14:47:49 +03:00
|
|
|
)
|
2021-02-05 19:09:24 +03:00
|
|
|
b := (*sliceHeader)(unsafe.Pointer(&buf)).data
|
2021-02-05 14:47:49 +03:00
|
|
|
for {
|
2021-02-05 19:09:24 +03:00
|
|
|
switch char(b, cursor) {
|
2021-02-05 14:47:49 +03:00
|
|
|
case ' ', '\n', '\t', '\r':
|
|
|
|
cursor++
|
|
|
|
case '"':
|
|
|
|
cursor++
|
2021-02-05 19:09:24 +03:00
|
|
|
c := char(b, cursor)
|
2021-02-05 14:47:49 +03:00
|
|
|
switch c {
|
|
|
|
case '"':
|
|
|
|
cursor++
|
2021-06-03 09:00:31 +03:00
|
|
|
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-04 14:58:58 +03:00
|
|
|
}
|
2021-02-05 14:47:49 +03:00
|
|
|
keyIdx := 0
|
2021-02-15 14:05:35 +03:00
|
|
|
bitmap := d.keyBitmapUint16
|
2021-02-06 14:07:01 +03:00
|
|
|
start := cursor
|
2021-02-04 14:58:58 +03:00
|
|
|
for {
|
2021-02-05 19:09:24 +03:00
|
|
|
c := char(b, cursor)
|
2021-02-04 14:58:58 +03:00
|
|
|
switch c {
|
|
|
|
case '"':
|
2021-02-15 14:05:35 +03:00
|
|
|
fieldSetIndex := bits.TrailingZeros16(curBit)
|
2021-06-03 09:00:31 +03:00
|
|
|
field := d.sortedFieldSets[fieldSetIndex]
|
2021-02-06 14:07:01 +03:00
|
|
|
keyLen := cursor - start
|
2021-02-04 14:58:58 +03:00
|
|
|
cursor++
|
2021-02-06 14:07:01 +03:00
|
|
|
if keyLen < field.keyLen {
|
|
|
|
// early match
|
|
|
|
return cursor, nil, nil
|
|
|
|
}
|
2021-02-05 14:47:49 +03:00
|
|
|
return cursor, field, nil
|
2021-02-04 14:58:58 +03:00
|
|
|
case nul:
|
2021-06-03 12:49:01 +03:00
|
|
|
return 0, nil, errors.ErrUnexpectedEndOfJSON("string", cursor)
|
2021-06-02 11:23:32 +03:00
|
|
|
case '\\':
|
|
|
|
cursor++
|
|
|
|
chars, nextCursor := decodeKeyCharByEscapedChar(buf, cursor)
|
|
|
|
for _, c := range chars {
|
|
|
|
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
|
|
|
|
if curBit == 0 {
|
2021-06-03 09:00:31 +03:00
|
|
|
return decodeKeyNotFound(b, cursor)
|
2021-06-02 11:23:32 +03:00
|
|
|
}
|
|
|
|
keyIdx++
|
|
|
|
}
|
|
|
|
cursor = nextCursor
|
2021-02-04 14:58:58 +03:00
|
|
|
default:
|
2021-02-05 14:47:49 +03:00
|
|
|
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
|
2021-02-04 14:58:58 +03:00
|
|
|
if curBit == 0 {
|
2021-06-03 09:00:31 +03:00
|
|
|
return decodeKeyNotFound(b, cursor)
|
2021-02-04 14:58:58 +03:00
|
|
|
}
|
|
|
|
keyIdx++
|
|
|
|
}
|
|
|
|
cursor++
|
|
|
|
}
|
|
|
|
default:
|
2021-06-03 12:49:01 +03:00
|
|
|
return cursor, nil, errors.ErrNotAtBeginningOfValue(cursor)
|
2021-02-04 14:58:58 +03:00
|
|
|
}
|
|
|
|
}
|
2021-02-05 14:47:49 +03:00
|
|
|
}
|
|
|
|
|
2021-06-03 09:00:31 +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 {
|
2021-06-03 13:38:01 +03:00
|
|
|
return 0, nil, errors.ErrUnexpectedEndOfJSON("string", cursor)
|
2021-06-03 09:00:31 +03:00
|
|
|
}
|
|
|
|
case nul:
|
2021-06-03 13:38:01 +03:00
|
|
|
return 0, nil, errors.ErrUnexpectedEndOfJSON("string", cursor)
|
2021-06-03 09:00:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04 14:58:58 +03:00
|
|
|
}
|
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
|
2021-02-04 14:58:58 +03:00
|
|
|
}
|
|
|
|
return cursor, field, nil
|
|
|
|
}
|
|
|
|
|
2021-06-03 12:49:01 +03:00
|
|
|
func decodeKeyByBitmapUint8Stream(d *structDecoder, s *Stream) (*structFieldSet, string, error) {
|
2021-02-06 08:24:07 +03:00
|
|
|
var (
|
2021-02-15 14:05:35 +03:00
|
|
|
curBit uint8 = math.MaxUint8
|
2021-02-06 08:24:07 +03:00
|
|
|
)
|
2021-06-03 09:00:31 +03:00
|
|
|
_, cursor, p := s.stat()
|
2021-02-06 08:24:07 +03:00
|
|
|
for {
|
2021-02-15 20:13:13 +03:00
|
|
|
switch char(p, cursor) {
|
2021-02-06 08:24:07 +03:00
|
|
|
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() {
|
2021-06-03 09:00:31 +03:00
|
|
|
_, cursor, p = s.stat()
|
2021-02-06 15:42:19 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-06-03 12:49:01 +03:00
|
|
|
return nil, "", errors.ErrNotAtBeginningOfValue(s.totalOffset())
|
2021-02-06 08:24:07 +03:00
|
|
|
case '"':
|
2021-02-15 20:13:13 +03:00
|
|
|
cursor++
|
2021-02-06 08:24:07 +03:00
|
|
|
FIRST_CHAR:
|
2021-02-15 20:13:13 +03:00
|
|
|
start := cursor
|
|
|
|
switch char(p, cursor) {
|
2021-02-06 08:24:07 +03:00
|
|
|
case '"':
|
2021-02-15 20:13:13 +03:00
|
|
|
cursor++
|
|
|
|
s.cursor = cursor
|
2021-06-03 09:00:31 +03:00
|
|
|
return nil, "", nil
|
2021-02-06 08:24:07 +03:00
|
|
|
case nul:
|
2021-02-15 20:13:13 +03:00
|
|
|
s.cursor = cursor
|
2021-02-06 08:24:07 +03:00
|
|
|
if s.read() {
|
2021-06-03 09:00:31 +03:00
|
|
|
_, cursor, p = s.stat()
|
2021-02-06 08:24:07 +03:00
|
|
|
goto FIRST_CHAR
|
|
|
|
}
|
2021-06-03 12:49:01 +03:00
|
|
|
return nil, "", errors.ErrUnexpectedEndOfJSON("string", s.totalOffset())
|
2021-02-06 08:24:07 +03:00
|
|
|
}
|
|
|
|
keyIdx := 0
|
2021-02-15 14:05:35 +03:00
|
|
|
bitmap := d.keyBitmapUint8
|
2021-02-06 08:24:07 +03:00
|
|
|
for {
|
2021-02-15 20:13:13 +03:00
|
|
|
c := char(p, cursor)
|
2021-02-06 08:24:07 +03:00
|
|
|
switch c {
|
|
|
|
case '"':
|
2021-02-15 14:05:35 +03:00
|
|
|
fieldSetIndex := bits.TrailingZeros8(curBit)
|
2021-06-03 09:00:31 +03:00
|
|
|
field := d.sortedFieldSets[fieldSetIndex]
|
2021-02-15 20:13:13 +03:00
|
|
|
keyLen := cursor - start
|
|
|
|
cursor++
|
|
|
|
s.cursor = cursor
|
2021-02-06 14:07:01 +03:00
|
|
|
if keyLen < field.keyLen {
|
|
|
|
// early match
|
|
|
|
return nil, field.key, nil
|
|
|
|
}
|
|
|
|
return field, field.key, nil
|
2021-02-06 08:24:07 +03:00
|
|
|
case nul:
|
2021-02-15 20:13:13 +03:00
|
|
|
s.cursor = cursor
|
2021-02-06 08:24:07 +03:00
|
|
|
if s.read() {
|
2021-06-03 09:00:31 +03:00
|
|
|
_, cursor, p = s.stat()
|
2021-02-06 08:24:07 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-06-03 12:49:01 +03:00
|
|
|
return nil, "", errors.ErrUnexpectedEndOfJSON("string", s.totalOffset())
|
2021-06-03 09:00:31 +03:00
|
|
|
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++
|
|
|
|
}
|
2021-02-06 08:24:07 +03:00
|
|
|
default:
|
|
|
|
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
|
|
|
|
if curBit == 0 {
|
2021-06-03 09:00:31 +03:00
|
|
|
s.cursor = cursor
|
|
|
|
return decodeKeyNotFoundStream(s, start)
|
2021-02-06 08:24:07 +03:00
|
|
|
}
|
|
|
|
keyIdx++
|
|
|
|
}
|
2021-02-15 20:13:13 +03:00
|
|
|
cursor++
|
2021-02-06 08:24:07 +03:00
|
|
|
}
|
|
|
|
default:
|
2021-06-03 12:49:01 +03:00
|
|
|
return nil, "", errors.ErrNotAtBeginningOfValue(s.totalOffset())
|
2021-02-06 08:24:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 12:49:01 +03:00
|
|
|
func decodeKeyByBitmapUint16Stream(d *structDecoder, s *Stream) (*structFieldSet, string, error) {
|
2021-02-06 08:24:07 +03:00
|
|
|
var (
|
2021-02-15 14:05:35 +03:00
|
|
|
curBit uint16 = math.MaxUint16
|
2021-02-06 08:24:07 +03:00
|
|
|
)
|
2021-06-03 09:00:31 +03:00
|
|
|
_, cursor, p := s.stat()
|
2021-02-06 08:24:07 +03:00
|
|
|
for {
|
2021-02-15 20:13:13 +03:00
|
|
|
switch char(p, cursor) {
|
2021-02-06 08:24:07 +03:00
|
|
|
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() {
|
2021-06-03 09:00:31 +03:00
|
|
|
_, cursor, p = s.stat()
|
2021-02-06 15:42:19 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-06-03 12:49:01 +03:00
|
|
|
return nil, "", errors.ErrNotAtBeginningOfValue(s.totalOffset())
|
2021-02-06 08:24:07 +03:00
|
|
|
case '"':
|
2021-02-15 20:13:13 +03:00
|
|
|
cursor++
|
2021-02-06 08:24:07 +03:00
|
|
|
FIRST_CHAR:
|
2021-02-15 20:13:13 +03:00
|
|
|
start := cursor
|
|
|
|
switch char(p, cursor) {
|
2021-02-06 08:24:07 +03:00
|
|
|
case '"':
|
2021-02-15 20:13:13 +03:00
|
|
|
cursor++
|
|
|
|
s.cursor = cursor
|
2021-06-03 09:00:31 +03:00
|
|
|
return nil, "", nil
|
2021-02-06 08:24:07 +03:00
|
|
|
case nul:
|
2021-02-15 20:13:13 +03:00
|
|
|
s.cursor = cursor
|
2021-02-06 08:24:07 +03:00
|
|
|
if s.read() {
|
2021-06-03 09:00:31 +03:00
|
|
|
_, cursor, p = s.stat()
|
2021-02-06 08:24:07 +03:00
|
|
|
goto FIRST_CHAR
|
|
|
|
}
|
2021-06-03 12:49:01 +03:00
|
|
|
return nil, "", errors.ErrUnexpectedEndOfJSON("string", s.totalOffset())
|
2021-02-06 08:24:07 +03:00
|
|
|
}
|
|
|
|
keyIdx := 0
|
2021-02-15 14:05:35 +03:00
|
|
|
bitmap := d.keyBitmapUint16
|
2021-02-06 08:24:07 +03:00
|
|
|
for {
|
2021-02-15 20:13:13 +03:00
|
|
|
c := char(p, cursor)
|
2021-02-06 08:24:07 +03:00
|
|
|
switch c {
|
|
|
|
case '"':
|
2021-02-15 14:05:35 +03:00
|
|
|
fieldSetIndex := bits.TrailingZeros16(curBit)
|
2021-06-03 09:00:31 +03:00
|
|
|
field := d.sortedFieldSets[fieldSetIndex]
|
2021-02-15 20:13:13 +03:00
|
|
|
keyLen := cursor - start
|
|
|
|
cursor++
|
|
|
|
s.cursor = cursor
|
2021-02-06 14:07:01 +03:00
|
|
|
if keyLen < field.keyLen {
|
|
|
|
// early match
|
|
|
|
return nil, field.key, nil
|
|
|
|
}
|
|
|
|
return field, field.key, nil
|
2021-02-06 08:24:07 +03:00
|
|
|
case nul:
|
2021-02-15 20:13:13 +03:00
|
|
|
s.cursor = cursor
|
2021-02-06 08:24:07 +03:00
|
|
|
if s.read() {
|
2021-06-03 09:00:31 +03:00
|
|
|
_, cursor, p = s.stat()
|
2021-02-06 08:24:07 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-06-03 12:49:01 +03:00
|
|
|
return nil, "", errors.ErrUnexpectedEndOfJSON("string", s.totalOffset())
|
2021-06-03 09:00:31 +03:00
|
|
|
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++
|
|
|
|
}
|
2021-02-06 08:24:07 +03:00
|
|
|
default:
|
|
|
|
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
|
|
|
|
if curBit == 0 {
|
2021-06-03 09:00:31 +03:00
|
|
|
s.cursor = cursor
|
|
|
|
return decodeKeyNotFoundStream(s, start)
|
2021-02-06 08:24:07 +03:00
|
|
|
}
|
|
|
|
keyIdx++
|
|
|
|
}
|
2021-02-15 20:13:13 +03:00
|
|
|
cursor++
|
2021-02-06 08:24:07 +03:00
|
|
|
}
|
|
|
|
default:
|
2021-06-03 12:49:01 +03:00
|
|
|
return nil, "", errors.ErrNotAtBeginningOfValue(s.totalOffset())
|
2021-02-06 08:24:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 09:00:31 +03:00
|
|
|
// decode from '\uXXXX'
|
2021-06-03 13:38:01 +03:00
|
|
|
func decodeKeyCharByUnicodeRuneStream(s *Stream) ([]byte, error) {
|
2021-06-03 09:00:31 +03:00
|
|
|
const defaultOffset = 4
|
|
|
|
const surrogateOffset = 6
|
|
|
|
|
|
|
|
if s.cursor+defaultOffset >= s.length {
|
|
|
|
if !s.read() {
|
2021-06-03 13:38:01 +03:00
|
|
|
return nil, errors.ErrInvalidCharacter(s.char(), "escaped unicode char", s.totalOffset())
|
2021-06-03 09:00:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-03 13:38:01 +03:00
|
|
|
func decodeKeyCharByEscapeCharStream(s *Stream) ([]byte, error) {
|
2021-06-03 09:00:31 +03:00
|
|
|
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() {
|
2021-06-03 13:38:01 +03:00
|
|
|
return nil, errors.ErrInvalidCharacter(s.char(), "escaped char", s.totalOffset())
|
2021-06-03 09:00:31 +03:00
|
|
|
}
|
|
|
|
goto RETRY
|
|
|
|
default:
|
2021-06-03 13:38:01 +03:00
|
|
|
return nil, errors.ErrUnexpectedEndOfJSON("struct field", s.totalOffset())
|
2021-06-03 09:00:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 13:38:01 +03:00
|
|
|
func decodeKeyNotFoundStream(s *Stream, start int64) (*structFieldSet, string, error) {
|
2021-06-03 09:00:31 +03:00
|
|
|
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() {
|
2021-06-03 13:38:01 +03:00
|
|
|
return nil, "", errors.ErrUnexpectedEndOfJSON("string", s.totalOffset())
|
2021-06-03 09:00:31 +03:00
|
|
|
}
|
|
|
|
buf, cursor, p = s.statForRetry()
|
|
|
|
}
|
|
|
|
case nul:
|
|
|
|
s.cursor = cursor
|
|
|
|
if !s.read() {
|
2021-06-03 13:38:01 +03:00
|
|
|
return nil, "", errors.ErrUnexpectedEndOfJSON("string", s.totalOffset())
|
2021-06-03 09:00:31 +03:00
|
|
|
}
|
|
|
|
buf, cursor, p = s.statForRetry()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 12:49:01 +03:00
|
|
|
func decodeKeyStream(d *structDecoder, s *Stream) (*structFieldSet, string, error) {
|
2021-02-06 08:24:07 +03:00
|
|
|
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 {
|
2021-02-18 13:05:06 +03:00
|
|
|
depth++
|
|
|
|
if depth > maxDecodeNestingDepth {
|
2021-06-03 12:49:01 +03:00
|
|
|
return errors.ErrExceededMaxDepth(s.char(), s.cursor)
|
2021-02-18 13:05:06 +03:00
|
|
|
}
|
|
|
|
|
2021-06-04 08:59:43 +03:00
|
|
|
c := s.skipWhiteSpace()
|
|
|
|
switch c {
|
2021-01-22 14:06:37 +03:00
|
|
|
case 'n':
|
|
|
|
if err := nullBytes(s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
if s.char() != '{' {
|
2021-06-03 12:49:01 +03:00
|
|
|
return errors.ErrNotAtBeginningOfValue(s.totalOffset())
|
2021-01-22 14:06:37 +03:00
|
|
|
}
|
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++
|
2020-11-24 14:15:11 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-06-06 05:24:56 +03:00
|
|
|
var (
|
|
|
|
seenFields map[int]struct{}
|
|
|
|
seenFieldNum int
|
|
|
|
)
|
2021-06-12 11:06:26 +03:00
|
|
|
firstWin := (s.Option.Flags & FirstWinOption) != 0
|
2021-06-06 05:24:56 +03:00
|
|
|
if firstWin {
|
|
|
|
seenFields = make(map[int]struct{}, d.fieldUniqueNameNum)
|
|
|
|
}
|
2020-07-30 16:41:53 +03:00
|
|
|
for {
|
|
|
|
s.reset()
|
2021-02-06 08:24:07 +03:00
|
|
|
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++
|
2021-02-06 08:24:07 +03:00
|
|
|
if field != nil {
|
2021-02-18 11:46:28 +03:00
|
|
|
if field.err != nil {
|
|
|
|
return field.err
|
|
|
|
}
|
2021-06-06 05:24:56 +03:00
|
|
|
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 {
|
2021-02-06 08:24:07 +03:00
|
|
|
return fmt.Errorf("json: unknown field %q", key)
|
2020-07-30 16:41:53 +03:00
|
|
|
} else {
|
2021-02-18 13:05:06 +03:00
|
|
|
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
|
2021-02-18 13:05:06 +03:00
|
|
|
depth++
|
|
|
|
if depth > maxDecodeNestingDepth {
|
2021-06-03 12:49:01 +03:00
|
|
|
return 0, errors.ErrExceededMaxDepth(buf[cursor], cursor)
|
2021-02-18 13:05:06 +03:00
|
|
|
}
|
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) {
|
2021-01-22 14:06:37 +03:00
|
|
|
case 'n':
|
2021-05-08 21:05:36 +03:00
|
|
|
if err := validateNull(buf, cursor); err != nil {
|
|
|
|
return 0, err
|
2021-01-22 14:06:37 +03:00
|
|
|
}
|
|
|
|
cursor += 4
|
|
|
|
return cursor, nil
|
|
|
|
case '{':
|
|
|
|
default:
|
2021-06-03 12:49:01 +03:00
|
|
|
return 0, errors.ErrNotAtBeginningOfValue(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
|
|
|
|
)
|
2021-06-12 11:06:26 +03:00
|
|
|
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
|
|
|
}
|
2021-02-04 14:58:58 +03:00
|
|
|
if field != nil {
|
2021-02-18 11:46:28 +03:00
|
|
|
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 {
|
2021-02-18 13:05:06 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|