go-json/decode_array.go

134 lines
2.7 KiB
Go
Raw Normal View History

2020-04-25 13:55:05 +03:00
package json
2020-12-24 15:52:40 +03:00
import (
"unsafe"
)
2020-11-19 06:47:42 +03:00
2020-04-25 13:55:05 +03:00
type arrayDecoder struct {
elemType *rtype
size uintptr
valueDecoder decoder
alen int
structName string
fieldName string
2020-04-25 13:55:05 +03:00
}
func newArrayDecoder(dec decoder, elemType *rtype, alen int, structName, fieldName string) *arrayDecoder {
2020-04-25 13:55:05 +03:00
return &arrayDecoder{
valueDecoder: dec,
elemType: elemType,
size: elemType.Size(),
alen: alen,
structName: structName,
fieldName: fieldName,
2020-04-25 13:55:05 +03:00
}
}
2020-11-19 06:47:42 +03:00
func (d *arrayDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
2020-07-30 16:41:53 +03:00
for {
switch s.char() {
case ' ', '\n', '\t', '\r':
2020-08-08 07:20:42 +03:00
case 'n':
if err := nullBytes(s); err != nil {
return err
}
return nil
2020-07-30 16:41:53 +03:00
case '[':
idx := 0
for {
2020-07-31 11:10:03 +03:00
s.cursor++
2020-12-24 15:52:40 +03:00
if idx < d.alen {
if err := d.valueDecoder.decodeStream(s, unsafe.Pointer(uintptr(p)+uintptr(idx)*d.size)); err != nil {
return err
}
} else {
if err := s.skipValue(); err != nil {
return err
}
2020-07-30 16:41:53 +03:00
}
s.skipWhiteSpace()
switch s.char() {
case ']':
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
return nil
case ',':
idx++
2020-07-31 11:10:03 +03:00
case nul:
if s.read() {
continue
}
goto ERROR
2020-07-30 16:41:53 +03:00
default:
2020-07-31 11:10:03 +03:00
goto ERROR
2020-07-30 16:41:53 +03:00
}
}
2020-07-31 11:10:03 +03:00
case nul:
if s.read() {
continue
}
goto ERROR
2020-07-30 16:41:53 +03:00
default:
2020-07-31 11:10:03 +03:00
goto ERROR
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
}
2020-07-31 11:10:03 +03:00
ERROR:
return errUnexpectedEndOfJSON("array", s.totalOffset())
2020-07-30 16:41:53 +03:00
}
2020-11-19 06:47:42 +03:00
func (d *arrayDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
2020-05-23 06:51:09 +03:00
buflen := int64(len(buf))
2020-04-25 13:55:05 +03:00
for ; cursor < buflen; cursor++ {
switch buf[cursor] {
case ' ', '\n', '\t', '\r':
continue
2020-08-08 07:20:42 +03:00
case 'n':
buflen := int64(len(buf))
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
2020-04-25 13:55:05 +03:00
case '[':
idx := 0
for {
2020-05-06 20:37:29 +03:00
cursor++
2020-12-24 17:14:58 +03:00
if idx < d.alen {
c, err := d.valueDecoder.decode(buf, cursor, unsafe.Pointer(uintptr(p)+uintptr(idx)*d.size))
if err != nil {
return 0, err
}
cursor = c
} else {
c, err := skipValue(buf, cursor)
if err != nil {
return 0, err
}
cursor = c
2020-04-25 13:55:05 +03:00
}
2020-05-06 20:37:29 +03:00
cursor = skipWhiteSpace(buf, cursor)
2020-04-25 13:55:05 +03:00
switch buf[cursor] {
case ']':
2020-05-06 20:37:29 +03:00
cursor++
return cursor, nil
2020-04-25 13:55:05 +03:00
case ',':
idx++
continue
default:
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor], "array", cursor)
2020-04-25 13:55:05 +03:00
}
}
}
}
2020-05-23 06:51:09 +03:00
return 0, errUnexpectedEndOfJSON("array", cursor)
2020-04-25 13:55:05 +03:00
}