go-json/decode_struct.go

427 lines
9.6 KiB
Go
Raw Permalink Normal View History

2020-04-23 19:39:20 +03:00
package json
import (
2020-08-11 13:05:20 +03:00
"fmt"
"math"
"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
2020-04-23 19:39:20 +03:00
}
type structDecoder struct {
2021-02-05 14:47:49 +03:00
fieldMap map[string]*structFieldSet
stringDecoder *stringDecoder
structName string
fieldName string
isTriedOptimize bool
keyBitmapInt8 [][256]int8
keyBitmapInt16 [][256]int16
sortedFieldSets []*structFieldSet
2021-02-05 18:54:10 +03:00
keyDecoder func(*structDecoder, *sliceHeader, int64) (int64, *structFieldSet, error)
}
var (
bitHashTable [64]int
largeToSmallTable [256]byte
)
func init() {
hash := uint64(0x03F566ED27179461)
for i := 0; i < 64; i++ {
bitHashTable[hash>>58] = i
hash <<= 1
}
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{
2021-02-05 14:47:49 +03:00
fieldMap: fieldMap,
stringDecoder: newStringDecoder(structName, fieldName),
structName: structName,
fieldName: fieldName,
keyDecoder: decodeKey,
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{}
for k, v := range d.fieldMap {
k := strings.ToLower(k)
fieldMap[k] = 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-05 14:47:49 +03:00
if len(sortedKeys) <= 8 {
keyBitmap := make([][256]int8, maxKeyLen)
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.keyBitmapInt8 = keyBitmap
d.keyDecoder = decodeKeyByBitmapInt8
} else {
keyBitmap := make([][256]int16, maxKeyLen)
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-05 14:47:49 +03:00
d.keyBitmapInt16 = keyBitmap
d.keyDecoder = decodeKeyByBitmapInt16
}
}
2021-02-05 18:54:10 +03:00
func decodeKeyByBitmapInt8(d *structDecoder, buf *sliceHeader, cursor int64) (int64, *structFieldSet, error) {
var (
field *structFieldSet
2021-02-05 14:47:49 +03:00
curBit int8 = math.MaxInt8
)
for {
2021-02-05 18:54:10 +03:00
switch char(buf.data, cursor) {
case ' ', '\n', '\t', '\r':
cursor++
case '"':
cursor++
2021-02-05 18:54:10 +03:00
c := char(buf.data, 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.keyBitmapInt8
keyBitmapLen := len(bitmap)
for {
2021-02-05 18:54:10 +03:00
c := char(buf.data, cursor)
2021-02-05 14:47:49 +03:00
switch c {
case '"':
x := uint64(curBit & -curBit)
fieldSetIndex := bitHashTable[(x*0x03F566ED27179461)>>58]
field = d.sortedFieldSets[fieldSetIndex]
cursor++
2021-02-05 14:47:49 +03:00
return cursor, field, nil
case nul:
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
default:
if keyIdx >= keyBitmapLen {
for {
cursor++
2021-02-05 18:54:10 +03:00
switch char(buf.data, cursor) {
2021-02-05 14:47:49 +03:00
case '"':
cursor++
return cursor, field, nil
case '\\':
cursor++
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor) == nul {
2021-02-05 14:47:49 +03:00
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
}
case nul:
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
}
}
}
2021-02-05 14:47:49 +03:00
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
if curBit == 0 {
for {
cursor++
2021-02-05 18:54:10 +03:00
switch char(buf.data, cursor) {
2021-02-05 14:47:49 +03:00
case '"':
cursor++
return cursor, field, nil
case '\\':
cursor++
2021-02-05 18:54:10 +03:00
if char(buf.data, 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)
}
}
}
2021-02-05 18:54:10 +03:00
func char(ptr unsafe.Pointer, offset int64) byte {
return *(*byte)(unsafe.Pointer(uintptr(ptr) + uintptr(offset)))
}
func decodeKeyByBitmapInt16(d *structDecoder, buf *sliceHeader, cursor int64) (int64, *structFieldSet, error) {
2021-02-05 14:47:49 +03:00
var (
field *structFieldSet
curBit int16 = math.MaxInt16
)
for {
2021-02-05 18:54:10 +03:00
switch char(buf.data, cursor) {
2021-02-05 14:47:49 +03:00
case ' ', '\n', '\t', '\r':
cursor++
case '"':
cursor++
2021-02-05 18:54:10 +03:00
c := char(buf.data, 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.keyBitmapInt16
keyBitmapLen := len(bitmap)
for {
2021-02-05 18:54:10 +03:00
c := char(buf.data, cursor)
switch c {
case '"':
x := uint64(curBit & -curBit)
fieldSetIndex := bitHashTable[(x*0x03F566ED27179461)>>58]
field = d.sortedFieldSets[fieldSetIndex]
cursor++
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
if keyIdx >= keyBitmapLen {
for {
cursor++
2021-02-05 18:54:10 +03:00
switch char(buf.data, cursor) {
case '"':
cursor++
2021-02-05 14:47:49 +03:00
return cursor, field, nil
case '\\':
cursor++
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor) == nul {
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
}
case nul:
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
}
}
}
2021-02-05 14:47:49 +03:00
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
if curBit == 0 {
for {
cursor++
2021-02-05 18:54:10 +03:00
switch char(buf.data, cursor) {
case '"':
cursor++
2021-02-05 14:47:49 +03:00
return cursor, field, nil
case '\\':
cursor++
2021-02-05 18:54:10 +03:00
if char(buf.data, 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
}
2021-02-05 18:54:10 +03:00
func decodeKey(d *structDecoder, buf *sliceHeader, cursor int64) (int64, *structFieldSet, error) {
2021-02-05 14:47:49 +03:00
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
}
2020-11-19 06:47:42 +03:00
func (d *structDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
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++
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()
2021-02-05 14:47:49 +03:00
key, err := d.stringDecoder.decodeStreamByte(s)
2020-07-30 16:41:53 +03:00
if err != nil {
return err
}
s.skipWhiteSpace()
2020-07-31 11:10:03 +03:00
if s.char() == nul {
s.read()
}
2020-07-30 16:41:53 +03:00
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
}
k := *(*string)(unsafe.Pointer(&key))
field, exists := d.fieldMap[k]
if exists {
if err := field.dec.decodeStream(s, 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 {
2020-08-11 13:05:20 +03:00
return fmt.Errorf("json: unknown field %q", k)
2020-07-30 16:41:53 +03:00
} else {
if err := s.skipValue(); err != nil {
return err
}
}
s.skipWhiteSpace()
2020-07-31 11:10:03 +03:00
if s.char() == nul {
s.read()
}
2020-07-30 16:41:53 +03:00
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
}
}
2021-02-05 18:54:10 +03:00
func (d *structDecoder) decode(buf *sliceHeader, cursor int64, p unsafe.Pointer) (int64, error) {
buflen := int64(buf.len)
2020-05-06 20:37:29 +03:00
cursor = skipWhiteSpace(buf, cursor)
2021-02-05 18:54:10 +03:00
switch char(buf.data, cursor) {
case 'n':
if cursor+3 >= buflen {
return 0, errUnexpectedEndOfJSON("null", cursor)
}
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor+1) != 'u' {
return 0, errInvalidCharacter(char(buf.data, cursor+1), "null", cursor)
}
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor+2) != 'l' {
return 0, errInvalidCharacter(char(buf.data, cursor+2), "null", cursor)
}
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor+3) != 'l' {
return 0, errInvalidCharacter(char(buf.data, cursor+3), "null", cursor)
}
cursor += 4
return cursor, nil
case '{':
default:
2020-05-23 06:51:09 +03:00
return 0, errNotAtBeginningOfValue(cursor)
}
if buflen < 2 {
return 0, errUnexpectedEndOfJSON("object", cursor)
2020-04-23 19:39:20 +03:00
}
cursor++
for ; cursor < buflen; cursor++ {
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
}
2020-05-06 20:37:29 +03:00
cursor = c
2021-02-05 14:47:49 +03:00
cursor = skipWhiteSpace(buf, cursor)
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor) != ':' {
2021-02-05 14:47:49 +03:00
return 0, errExpected("colon after object key", cursor)
}
cursor++
2021-02-05 18:54:10 +03:00
if cursor >= buflen {
2021-02-05 14:47:49 +03:00
return 0, errExpected("object value after colon", cursor)
}
if field != nil {
c, err := field.dec.decode(buf, cursor, 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 {
2020-05-08 14:22:57 +03:00
c, err := skipValue(buf, cursor)
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 18:54:10 +03:00
if char(buf.data, cursor) == '}' {
2020-05-06 20:37:29 +03:00
cursor++
return cursor, nil
2020-04-23 19:39:20 +03:00
}
2021-02-05 18:54:10 +03:00
if char(buf.data, 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
}
}
2020-05-06 20:37:29 +03:00
return cursor, nil
2020-04-23 19:39:20 +03:00
}