2020-04-23 19:39:20 +03:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2020-08-11 13:05:20 +03:00
|
|
|
"fmt"
|
2020-04-23 19:39:20 +03:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type structFieldSet struct {
|
|
|
|
dec decoder
|
|
|
|
offset uintptr
|
|
|
|
}
|
|
|
|
|
|
|
|
type structDecoder struct {
|
2020-08-11 13:05:20 +03:00
|
|
|
fieldMap map[string]*structFieldSet
|
|
|
|
keyDecoder *stringDecoder
|
|
|
|
disallowUnknownFields bool
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newStructDecoder(fieldMap map[string]*structFieldSet) *structDecoder {
|
|
|
|
return &structDecoder{
|
|
|
|
fieldMap: fieldMap,
|
|
|
|
keyDecoder: newStringDecoder(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 13:05:20 +03:00
|
|
|
func (d *structDecoder) setDisallowUnknownFields(disallowUnknownFields bool) {
|
|
|
|
d.disallowUnknownFields = disallowUnknownFields
|
|
|
|
for _, field := range d.fieldMap {
|
|
|
|
field.dec.setDisallowUnknownFields(disallowUnknownFields)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 16:41:53 +03:00
|
|
|
func (d *structDecoder) decodeStream(s *stream, p uintptr) error {
|
|
|
|
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 errNotAtBeginningOfValue(s.totalOffset())
|
|
|
|
}
|
2020-07-31 11:10:03 +03:00
|
|
|
s.cursor++
|
2020-07-30 16:41:53 +03:00
|
|
|
for {
|
|
|
|
s.reset()
|
|
|
|
key, err := d.keyDecoder.decodeStreamByte(s)
|
|
|
|
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 {
|
|
|
|
s.read()
|
|
|
|
}
|
2020-07-30 16:41:53 +03:00
|
|
|
if s.end() {
|
|
|
|
return errExpected("object value after colon", s.totalOffset())
|
|
|
|
}
|
|
|
|
k := *(*string)(unsafe.Pointer(&key))
|
|
|
|
field, exists := d.fieldMap[k]
|
|
|
|
if exists {
|
|
|
|
if err := field.dec.decodeStream(s, p+field.offset); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-11 13:05:20 +03:00
|
|
|
} else if d.disallowUnknownFields {
|
|
|
|
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
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-23 06:51:09 +03:00
|
|
|
func (d *structDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) {
|
|
|
|
buflen := int64(len(buf))
|
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-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++ {
|
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-23 06:51:09 +03:00
|
|
|
return 0, errExpected("colon after object key", cursor)
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
|
|
|
cursor++
|
|
|
|
if cursor >= buflen {
|
2020-05-23 06:51:09 +03:00
|
|
|
return 0, errExpected("object value after colon", cursor)
|
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-08-11 13:05:20 +03:00
|
|
|
} else if d.disallowUnknownFields {
|
|
|
|
return 0, fmt.Errorf("json: unknown field %q", k)
|
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)
|
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-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
|
|
|
}
|