go-json/internal/decoder/array.go

150 lines
3.1 KiB
Go
Raw Normal View History

2021-06-03 12:49:01 +03:00
package decoder
2020-04-25 13:55:05 +03:00
2020-12-24 15:52:40 +03:00
import (
"unsafe"
2021-06-03 12:49:01 +03:00
"github.com/goccy/go-json/internal/errors"
"github.com/goccy/go-json/internal/runtime"
2020-12-24 15:52:40 +03:00
)
2020-11-19 06:47:42 +03:00
2020-04-25 13:55:05 +03:00
type arrayDecoder struct {
2021-06-03 12:49:01 +03:00
elemType *runtime.Type
2020-04-25 13:55:05 +03:00
size uintptr
2021-06-03 13:10:17 +03:00
valueDecoder Decoder
2020-04-25 13:55:05 +03:00
alen int
structName string
fieldName string
2021-02-18 06:13:49 +03:00
zeroValue unsafe.Pointer
2020-04-25 13:55:05 +03:00
}
2021-06-03 13:10:17 +03:00
func newArrayDecoder(dec Decoder, elemType *runtime.Type, alen int, structName, fieldName string) *arrayDecoder {
2021-02-18 06:13:49 +03:00
zeroValue := *(*unsafe.Pointer)(unsafe_New(elemType))
2020-04-25 13:55:05 +03:00
return &arrayDecoder{
valueDecoder: dec,
elemType: elemType,
size: elemType.Size(),
alen: alen,
structName: structName,
fieldName: fieldName,
2021-02-18 06:13:49 +03:00
zeroValue: zeroValue,
2020-04-25 13:55:05 +03:00
}
}
2021-06-03 12:49:01 +03:00
func (d *arrayDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error {
depth++
if depth > maxDecodeNestingDepth {
2021-06-03 12:49:01 +03:00
return errors.ErrExceededMaxDepth(s.char(), s.cursor)
}
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 {
2021-06-03 12:49:01 +03:00
if err := d.valueDecoder.DecodeStream(s, depth, unsafe.Pointer(uintptr(p)+uintptr(idx)*d.size)); err != nil {
2020-12-24 15:52:40 +03:00
return err
}
} else {
if err := s.skipValue(depth); err != nil {
2020-12-24 15:52:40 +03:00
return err
}
2020-07-30 16:41:53 +03:00
}
2021-02-18 06:13:49 +03:00
idx++
2021-06-04 08:59:43 +03:00
switch s.skipWhiteSpace() {
2020-07-30 16:41:53 +03:00
case ']':
2021-02-18 06:13:49 +03:00
for idx < d.alen {
*(*unsafe.Pointer)(unsafe.Pointer(uintptr(p) + uintptr(idx)*d.size)) = d.zeroValue
idx++
}
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
return nil
case ',':
2021-02-18 06:13:49 +03:00
continue
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:
2021-06-03 12:49:01 +03:00
return errors.ErrUnexpectedEndOfJSON("array", s.totalOffset())
2020-07-30 16:41:53 +03:00
}
2021-06-04 19:08:27 +03:00
func (d *arrayDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
buf := ctx.Buf
depth++
if depth > maxDecodeNestingDepth {
2021-06-03 12:49:01 +03:00
return 0, errors.ErrExceededMaxDepth(buf[cursor], cursor)
}
2021-05-08 21:05:36 +03:00
for {
2020-04-25 13:55:05 +03:00
switch buf[cursor] {
case ' ', '\n', '\t', '\r':
2021-05-08 21:05:36 +03:00
cursor++
2020-04-25 13:55:05 +03:00
continue
2020-08-08 07:20:42 +03:00
case 'n':
2021-05-08 21:05:36 +03:00
if err := validateNull(buf, cursor); err != nil {
return 0, err
2020-08-08 07:20:42 +03:00
}
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 {
2021-06-04 19:08:27 +03:00
c, err := d.valueDecoder.Decode(ctx, cursor, depth, unsafe.Pointer(uintptr(p)+uintptr(idx)*d.size))
2020-12-24 17:14:58 +03:00
if err != nil {
return 0, err
}
cursor = c
} else {
c, err := skipValue(buf, cursor, depth)
2020-12-24 17:14:58 +03:00
if err != nil {
return 0, err
}
cursor = c
2020-04-25 13:55:05 +03:00
}
2021-02-18 06:13:49 +03:00
idx++
2020-05-06 20:37:29 +03:00
cursor = skipWhiteSpace(buf, cursor)
2020-04-25 13:55:05 +03:00
switch buf[cursor] {
case ']':
2021-02-18 06:13:49 +03:00
for idx < d.alen {
*(*unsafe.Pointer)(unsafe.Pointer(uintptr(p) + uintptr(idx)*d.size)) = d.zeroValue
idx++
}
2020-05-06 20:37:29 +03:00
cursor++
return cursor, nil
2020-04-25 13:55:05 +03:00
case ',':
continue
default:
2021-06-03 12:49:01 +03:00
return 0, errors.ErrInvalidCharacter(buf[cursor], "array", cursor)
2020-04-25 13:55:05 +03:00
}
}
2021-05-08 21:05:36 +03:00
default:
2021-06-03 12:49:01 +03:00
return 0, errors.ErrUnexpectedEndOfJSON("array", cursor)
2020-04-25 13:55:05 +03:00
}
}
}