go-json/decode_struct.go

633 lines
14 KiB
Go
Raw Normal View History

2020-04-23 19:39:20 +03:00
package json
import (
2020-08-11 13:05:20 +03:00
"fmt"
"math"
"math/bits"
"sort"
"strings"
2020-04-23 19:39:20 +03:00
"unsafe"
)
type structFieldSet struct {
2020-12-22 15:55:59 +03:00
dec decoder
offset uintptr
isTaggedKey bool
key string
keyLen int64
err error
2020-04-23 19:39:20 +03:00
}
type structDecoder struct {
fieldMap map[string]*structFieldSet
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 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() {
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 {
// 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
}
}
func decodeKeyByBitmapUint8(d *structDecoder, buf []byte, cursor int64) (int64, *structFieldSet, error) {
var (
field *structFieldSet
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++
2021-02-05 14:47:49 +03:00
return cursor, field, nil
case nul:
return 0, nil, 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)
2021-02-05 14:47:49 +03:00
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:
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
default:
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
if curBit == 0 {
for {
cursor++
switch char(b, cursor) {
2021-02-05 14:47:49 +03:00
case '"':
cursor++
return cursor, field, nil
case '\\':
cursor++
if char(b, cursor) == nul {
2021-02-05 14:47:49 +03:00
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
}
case nul:
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
}
}
}
keyIdx++
}
2021-02-05 14:47:49 +03:00
cursor++
}
default:
return cursor, nil, errNotAtBeginningOfValue(cursor)
}
}
}
func decodeKeyByBitmapUint16(d *structDecoder, buf []byte, cursor int64) (int64, *structFieldSet, error) {
2021-02-05 14:47:49 +03:00
var (
field *structFieldSet
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, field, nil
case nul:
return 0, nil, 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:
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
default:
2021-02-05 14:47:49 +03:00
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
if curBit == 0 {
for {
cursor++
switch char(b, cursor) {
case '"':
cursor++
2021-02-05 14:47:49 +03:00
return cursor, field, nil
case '\\':
cursor++
if char(b, cursor) == nul {
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
}
case nul:
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
}
}
}
keyIdx++
}
cursor++
}
default:
return cursor, nil, errNotAtBeginningOfValue(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
}
func decodeKeyByBitmapUint8Stream(d *structDecoder, s *stream) (*structFieldSet, string, error) {
var (
field *structFieldSet
curBit uint8 = math.MaxUint8
)
2021-02-15 20:13:13 +03:00
buf, 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() {
2021-02-15 20:13:13 +03:00
buf, cursor, p = s.stat()
2021-02-06 15:42:19 +03:00
continue
}
return nil, "", errNotAtBeginningOfValue(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 field, "", nil
case nul:
2021-02-15 20:13:13 +03:00
s.cursor = cursor
if s.read() {
2021-02-15 20:13:13 +03:00
buf, cursor, p = s.stat()
goto FIRST_CHAR
}
return nil, "", 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() {
2021-02-15 20:13:13 +03:00
buf, cursor, p = s.stat()
continue
}
return nil, "", errUnexpectedEndOfJSON("string", s.totalOffset())
default:
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
if curBit == 0 {
for {
2021-02-15 20:13:13 +03:00
cursor++
switch char(p, cursor) {
case '"':
2021-02-15 20:13:13 +03:00
b := buf[start:cursor]
key := *(*string)(unsafe.Pointer(&b))
2021-02-15 20:13:13 +03:00
cursor++
s.cursor = cursor
return field, key, nil
case '\\':
2021-02-15 20:13:13 +03:00
cursor++
if char(p, cursor) == nul {
s.cursor = cursor
if !s.read() {
return nil, "", errUnexpectedEndOfJSON("string", s.totalOffset())
}
2021-05-03 10:25:26 +03:00
buf, cursor, p = s.statForRetry()
}
case nul:
2021-02-15 20:13:13 +03:00
s.cursor = cursor
if !s.read() {
return nil, "", errUnexpectedEndOfJSON("string", s.totalOffset())
}
2021-05-03 10:25:26 +03:00
buf, cursor, p = s.statForRetry()
}
}
}
keyIdx++
}
2021-02-15 20:13:13 +03:00
cursor++
}
default:
return nil, "", errNotAtBeginningOfValue(s.totalOffset())
}
}
}
func decodeKeyByBitmapUint16Stream(d *structDecoder, s *stream) (*structFieldSet, string, error) {
var (
field *structFieldSet
curBit uint16 = math.MaxUint16
)
2021-02-15 20:13:13 +03:00
buf, 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() {
2021-02-15 20:13:13 +03:00
buf, cursor, p = s.stat()
2021-02-06 15:42:19 +03:00
continue
}
return nil, "", errNotAtBeginningOfValue(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 field, "", nil
case nul:
2021-02-15 20:13:13 +03:00
s.cursor = cursor
if s.read() {
2021-02-15 20:13:13 +03:00
buf, cursor, p = s.stat()
goto FIRST_CHAR
}
return nil, "", 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() {
2021-02-15 20:13:13 +03:00
buf, cursor, p = s.stat()
continue
}
return nil, "", errUnexpectedEndOfJSON("string", s.totalOffset())
default:
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
if curBit == 0 {
for {
2021-02-15 20:13:13 +03:00
cursor++
switch char(p, cursor) {
case '"':
2021-02-15 20:13:13 +03:00
b := buf[start:cursor]
key := *(*string)(unsafe.Pointer(&b))
2021-02-15 20:13:13 +03:00
cursor++
s.cursor = cursor
return field, key, nil
case '\\':
2021-02-15 20:13:13 +03:00
cursor++
if char(p, cursor) == nul {
s.cursor = cursor
if !s.read() {
return nil, "", errUnexpectedEndOfJSON("string", s.totalOffset())
}
2021-05-03 10:25:26 +03:00
buf, cursor, p = s.statForRetry()
}
case nul:
2021-02-15 20:13:13 +03:00
s.cursor = cursor
if !s.read() {
return nil, "", errUnexpectedEndOfJSON("string", s.totalOffset())
}
2021-05-03 10:25:26 +03:00
buf, cursor, p = s.statForRetry()
}
}
}
keyIdx++
}
2021-02-15 20:13:13 +03:00
cursor++
}
default:
return nil, "", errNotAtBeginningOfValue(s.totalOffset())
}
}
}
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
}
func (d *structDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
depth++
if depth > maxDecodeNestingDepth {
return errExceededMaxDepth(s.char(), s.cursor)
}
2020-07-30 16:41:53 +03:00
s.skipWhiteSpace()
switch s.char() {
case 'n':
if err := nullBytes(s); err != nil {
return err
}
return nil
case nul:
2020-07-31 11:10:03 +03:00
s.read()
default:
if s.char() != '{' {
return errNotAtBeginningOfValue(s.totalOffset())
}
2020-07-30 16:41:53 +03:00
}
2020-07-31 11:10:03 +03:00
s.cursor++
2021-02-15 12:45:58 +03:00
s.skipWhiteSpace()
if s.char() == '}' {
2020-12-24 11:47:53 +03:00
s.cursor++
return nil
}
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
}
s.skipWhiteSpace()
if s.char() != ':' {
return errExpected("colon after object key", s.totalOffset())
}
2020-07-31 11:10:03 +03:00
s.cursor++
if s.char() == nul {
2020-12-05 16:27:33 +03:00
if !s.read() {
return errExpected("object value after colon", s.totalOffset())
}
2020-07-30 16:41:53 +03:00
}
if field != nil {
if field.err != nil {
return field.err
}
if err := field.dec.decodeStream(s, depth, unsafe.Pointer(uintptr(p)+field.offset)); err != nil {
2020-07-30 16:41:53 +03:00
return err
}
2020-08-14 11:59:49 +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
}
}
s.skipWhiteSpace()
c := s.char()
if c == '}' {
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
return nil
}
if c != ',' {
return errExpected("comma after object element", s.totalOffset())
}
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
}
}
func (d *structDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
depth++
if depth > maxDecodeNestingDepth {
return 0, 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':
if cursor+3 >= buflen {
return 0, errUnexpectedEndOfJSON("null", cursor)
}
if buf[cursor+1] != 'u' {
return 0, errInvalidCharacter(buf[cursor+1], "null", cursor)
}
if buf[cursor+2] != 'l' {
return 0, errInvalidCharacter(buf[cursor+2], "null", cursor)
}
if buf[cursor+3] != 'l' {
return 0, errInvalidCharacter(buf[cursor+3], "null", cursor)
}
cursor += 4
return cursor, nil
case '{':
default:
2020-05-23 06:51:09 +03:00
return 0, errNotAtBeginningOfValue(cursor)
}
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-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-02-05 14:47:49 +03:00
return 0, errExpected("colon after object key", cursor)
}
cursor++
2021-02-05 19:19:24 +03:00
if cursor >= buflen {
2021-02-05 14:47:49 +03:00
return 0, errExpected("object value after colon", cursor)
}
if field != nil {
if field.err != nil {
return 0, field.err
}
c, err := field.dec.decode(buf, cursor, depth, unsafe.Pointer(uintptr(p)+field.offset))
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
} 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) != ',' {
2020-05-23 06:51:09 +03:00
return 0, 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
}
}