go-json/decode_array.go

54 lines
1021 B
Go
Raw Normal View History

2020-04-25 13:55:05 +03:00
package json
import (
"errors"
)
type arrayDecoder struct {
elemType *rtype
size uintptr
valueDecoder decoder
alen int
}
func newArrayDecoder(dec decoder, elemType *rtype, alen int) *arrayDecoder {
return &arrayDecoder{
valueDecoder: dec,
elemType: elemType,
size: elemType.Size(),
alen: alen,
}
}
2020-05-06 20:37:29 +03:00
func (d *arrayDecoder) decode(buf []byte, cursor int, p uintptr) (int, error) {
buflen := len(buf)
2020-04-25 13:55:05 +03:00
for ; cursor < buflen; cursor++ {
switch buf[cursor] {
case ' ', '\n', '\t', '\r':
continue
case '[':
idx := 0
for {
2020-05-06 20:37:29 +03:00
cursor++
c, err := d.valueDecoder.decode(buf, cursor, p+uintptr(idx)*d.size)
if err != nil {
return 0, err
2020-04-25 13:55:05 +03:00
}
2020-05-06 20:37:29 +03:00
cursor = c
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-06 20:37:29 +03:00
return 0, errors.New("syntax error array")
2020-04-25 13:55:05 +03:00
}
}
}
}
2020-05-06 20:37:29 +03:00
return 0, errors.New("unexpected error array")
2020-04-25 13:55:05 +03:00
}