2020-04-23 19:39:20 +03:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type structFieldSet struct {
|
|
|
|
dec decoder
|
|
|
|
offset uintptr
|
|
|
|
}
|
|
|
|
|
|
|
|
type structDecoder struct {
|
|
|
|
fieldMap map[string]*structFieldSet
|
|
|
|
keyDecoder *stringDecoder
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStructDecoder(fieldMap map[string]*structFieldSet) *structDecoder {
|
|
|
|
return &structDecoder{
|
|
|
|
fieldMap: fieldMap,
|
|
|
|
keyDecoder: newStringDecoder(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 20:37:29 +03:00
|
|
|
func (d *structDecoder) skipValue(buf []byte, cursor int) (int, error) {
|
|
|
|
cursor = skipWhiteSpace(buf, cursor)
|
2020-04-23 19:39:20 +03:00
|
|
|
braceCount := 0
|
|
|
|
bracketCount := 0
|
2020-05-06 20:37:29 +03:00
|
|
|
buflen := len(buf)
|
2020-05-07 07:51:17 +03:00
|
|
|
for {
|
2020-04-23 19:39:20 +03:00
|
|
|
switch buf[cursor] {
|
2020-05-07 07:51:17 +03:00
|
|
|
case '\000':
|
|
|
|
return cursor, errors.New("unexpected error value")
|
2020-04-23 19:39:20 +03:00
|
|
|
case '{':
|
|
|
|
braceCount++
|
|
|
|
case '[':
|
|
|
|
bracketCount++
|
|
|
|
case '}':
|
|
|
|
braceCount--
|
|
|
|
if braceCount == -1 && bracketCount == 0 {
|
2020-05-06 20:37:29 +03:00
|
|
|
return cursor, nil
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
|
|
|
case ']':
|
|
|
|
bracketCount--
|
|
|
|
case ',':
|
|
|
|
if bracketCount == 0 && braceCount == 0 {
|
2020-05-06 20:37:29 +03:00
|
|
|
return cursor, nil
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
2020-04-26 08:59:45 +03:00
|
|
|
case '"':
|
|
|
|
cursor++
|
2020-05-07 14:02:55 +03:00
|
|
|
|
2020-04-26 08:59:45 +03:00
|
|
|
for ; cursor < buflen; cursor++ {
|
2020-05-07 14:02:55 +03:00
|
|
|
if buf[cursor] != '"' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if buf[cursor-1] == '\\' {
|
|
|
|
continue
|
2020-04-26 08:59:45 +03:00
|
|
|
}
|
2020-05-07 14:02:55 +03:00
|
|
|
if bracketCount == 0 && braceCount == 0 {
|
|
|
|
return cursor + 1, nil
|
|
|
|
}
|
|
|
|
break
|
2020-04-26 08:59:45 +03:00
|
|
|
}
|
2020-05-07 14:02:55 +03:00
|
|
|
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
2020-05-07 07:51:17 +03:00
|
|
|
cursor++
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
2020-05-06 20:37:29 +03:00
|
|
|
return cursor, errors.New("unexpected error value")
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
|
|
|
|
2020-05-06 20:37:29 +03:00
|
|
|
func (d *structDecoder) decode(buf []byte, cursor int, p uintptr) (int, error) {
|
|
|
|
buflen := len(buf)
|
|
|
|
cursor = skipWhiteSpace(buf, cursor)
|
2020-04-23 19:39:20 +03:00
|
|
|
if buflen < 2 {
|
2020-05-06 20:37:29 +03:00
|
|
|
return 0, errors.New("unexpected error {}")
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
|
|
|
if buf[cursor] != '{' {
|
2020-05-06 20:37:29 +03:00
|
|
|
return 0, errors.New("unexpected error {")
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
|
|
|
cursor++
|
|
|
|
for ; cursor < buflen; cursor++ {
|
2020-05-06 20:37:29 +03:00
|
|
|
key, c, err := d.keyDecoder.decodeByte(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
|
|
|
|
cursor = skipWhiteSpace(buf, cursor)
|
2020-04-23 19:39:20 +03:00
|
|
|
if buf[cursor] != ':' {
|
2020-05-06 20:37:29 +03:00
|
|
|
return 0, errors.New("unexpected error invalid delimiter for object")
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
|
|
|
cursor++
|
|
|
|
if cursor >= buflen {
|
2020-05-06 20:37:29 +03:00
|
|
|
return 0, errors.New("unexpected error missing value")
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
|
|
|
k := *(*string)(unsafe.Pointer(&key))
|
|
|
|
field, exists := d.fieldMap[k]
|
|
|
|
if exists {
|
2020-05-06 20:37:29 +03:00
|
|
|
c, err := field.dec.decode(buf, cursor, p+field.offset)
|
|
|
|
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-06 20:37:29 +03:00
|
|
|
c, err := d.skipValue(buf, cursor)
|
|
|
|
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)
|
2020-04-23 19:39:20 +03:00
|
|
|
if buf[cursor] == '}' {
|
2020-05-06 20:37:29 +03:00
|
|
|
cursor++
|
|
|
|
return cursor, nil
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
|
|
|
if buf[cursor] != ',' {
|
2020-05-06 20:37:29 +03:00
|
|
|
return 0, errors.New("unexpected error ,")
|
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
|
|
|
}
|