2020-04-25 13:55:05 +03:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2021-02-09 15:37:18 +03:00
|
|
|
"reflect"
|
2020-05-07 15:30:36 +03:00
|
|
|
"sync"
|
2020-04-25 13:55:05 +03:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type sliceDecoder struct {
|
2021-04-30 16:55:08 +03:00
|
|
|
elemType *rtype
|
|
|
|
isElemPointerType bool
|
|
|
|
isElemUnmarshalJSONType bool
|
|
|
|
valueDecoder decoder
|
|
|
|
size uintptr
|
|
|
|
arrayPool sync.Pool
|
|
|
|
structName string
|
|
|
|
fieldName string
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
|
|
|
|
2020-05-30 18:11:50 +03:00
|
|
|
// If use reflect.SliceHeader, data type is uintptr.
|
|
|
|
// In this case, Go compiler cannot trace reference created by newArray().
|
|
|
|
// So, define using unsafe.Pointer as data type
|
|
|
|
type sliceHeader struct {
|
|
|
|
data unsafe.Pointer
|
|
|
|
len int
|
|
|
|
cap int
|
|
|
|
}
|
|
|
|
|
2021-02-03 17:50:02 +03:00
|
|
|
const (
|
|
|
|
defaultSliceCapacity = 2
|
|
|
|
)
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
func newSliceDecoder(dec decoder, elemType *rtype, size uintptr, structName, fieldName string) *sliceDecoder {
|
2020-04-25 13:55:05 +03:00
|
|
|
return &sliceDecoder{
|
2021-04-30 16:55:08 +03:00
|
|
|
valueDecoder: dec,
|
|
|
|
elemType: elemType,
|
|
|
|
isElemPointerType: elemType.Kind() == reflect.Ptr || elemType.Kind() == reflect.Map,
|
|
|
|
isElemUnmarshalJSONType: rtype_ptrTo(elemType).Implements(unmarshalJSONType),
|
|
|
|
size: size,
|
2020-05-07 15:30:36 +03:00
|
|
|
arrayPool: sync.Pool{
|
|
|
|
New: func() interface{} {
|
2020-05-30 18:11:50 +03:00
|
|
|
return &sliceHeader{
|
2021-02-03 17:50:02 +03:00
|
|
|
data: newArray(elemType, defaultSliceCapacity),
|
2020-05-30 18:11:50 +03:00
|
|
|
len: 0,
|
2021-02-03 17:50:02 +03:00
|
|
|
cap: defaultSliceCapacity,
|
2020-05-07 15:30:36 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2020-11-23 11:16:31 +03:00
|
|
|
structName: structName,
|
|
|
|
fieldName: fieldName,
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 18:11:50 +03:00
|
|
|
func (d *sliceDecoder) newSlice() *sliceHeader {
|
|
|
|
slice := d.arrayPool.Get().(*sliceHeader)
|
|
|
|
slice.len = 0
|
2020-05-07 15:30:36 +03:00
|
|
|
return slice
|
|
|
|
}
|
|
|
|
|
2020-05-30 18:11:50 +03:00
|
|
|
func (d *sliceDecoder) releaseSlice(p *sliceHeader) {
|
2020-05-07 15:30:36 +03:00
|
|
|
d.arrayPool.Put(p)
|
|
|
|
}
|
|
|
|
|
2020-04-25 13:55:05 +03:00
|
|
|
//go:linkname copySlice reflect.typedslicecopy
|
2020-11-14 21:24:34 +03:00
|
|
|
func copySlice(elemType *rtype, dst, src sliceHeader) int
|
2020-04-25 13:55:05 +03:00
|
|
|
|
|
|
|
//go:linkname newArray reflect.unsafe_NewArray
|
|
|
|
func newArray(*rtype, int) unsafe.Pointer
|
|
|
|
|
2021-02-09 16:20:23 +03:00
|
|
|
func (d *sliceDecoder) errNumber(offset int64) *UnmarshalTypeError {
|
2021-02-09 15:37:18 +03:00
|
|
|
return &UnmarshalTypeError{
|
|
|
|
Value: "number",
|
|
|
|
Type: reflect.SliceOf(rtype2type(d.elemType)),
|
|
|
|
Struct: d.structName,
|
|
|
|
Field: d.fieldName,
|
|
|
|
Offset: offset,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:05:06 +03:00
|
|
|
func (d *sliceDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
|
|
|
|
depth++
|
|
|
|
if depth > maxDecodeNestingDepth {
|
|
|
|
return errExceededMaxDepth(s.char(), s.cursor)
|
|
|
|
}
|
|
|
|
|
2020-07-30 16:41:53 +03:00
|
|
|
for {
|
|
|
|
switch s.char() {
|
|
|
|
case ' ', '\n', '\t', '\r':
|
2020-07-31 11:10:03 +03:00
|
|
|
s.cursor++
|
2020-07-30 16:41:53 +03:00
|
|
|
continue
|
2020-08-08 07:20:42 +03:00
|
|
|
case 'n':
|
|
|
|
if err := nullBytes(s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-16 19:51:42 +03:00
|
|
|
*(*unsafe.Pointer)(p) = nil
|
2020-08-08 07:20:42 +03:00
|
|
|
return nil
|
2020-07-30 16:41:53 +03:00
|
|
|
case '[':
|
2020-08-12 10:54:15 +03:00
|
|
|
s.cursor++
|
|
|
|
s.skipWhiteSpace()
|
|
|
|
if s.char() == ']' {
|
2020-12-05 16:27:33 +03:00
|
|
|
*(*sliceHeader)(p) = sliceHeader{
|
2020-11-14 21:24:34 +03:00
|
|
|
data: newArray(d.elemType, 0),
|
|
|
|
len: 0,
|
|
|
|
cap: 0,
|
2020-08-12 10:54:15 +03:00
|
|
|
}
|
|
|
|
s.cursor++
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-30 16:41:53 +03:00
|
|
|
idx := 0
|
|
|
|
slice := d.newSlice()
|
2021-02-03 17:50:02 +03:00
|
|
|
capacity := slice.cap
|
2020-07-31 12:52:22 +03:00
|
|
|
data := slice.data
|
2020-07-31 11:10:03 +03:00
|
|
|
for {
|
2021-02-03 17:50:02 +03:00
|
|
|
if capacity <= idx {
|
|
|
|
src := sliceHeader{data: data, len: idx, cap: capacity}
|
|
|
|
capacity *= 2
|
|
|
|
data = newArray(d.elemType, capacity)
|
|
|
|
dst := sliceHeader{data: data, len: idx, cap: capacity}
|
2020-07-30 16:41:53 +03:00
|
|
|
copySlice(d.elemType, dst, src)
|
|
|
|
}
|
2021-03-28 20:28:04 +03:00
|
|
|
ep := unsafe.Pointer(uintptr(data) + uintptr(idx)*d.size)
|
2021-04-30 16:55:08 +03:00
|
|
|
if d.isElemUnmarshalJSONType {
|
|
|
|
receiver := unsafe_New(d.elemType)
|
|
|
|
if err := d.valueDecoder.decodeStream(s, depth, receiver); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*(*unsafe.Pointer)(ep) = **(**unsafe.Pointer)(unsafe.Pointer(&receiver))
|
|
|
|
} else {
|
|
|
|
if d.isElemPointerType {
|
|
|
|
*(*unsafe.Pointer)(ep) = nil // initialize elem pointer
|
|
|
|
}
|
|
|
|
if err := d.valueDecoder.decodeStream(s, depth, ep); err != nil {
|
|
|
|
return err
|
2021-04-29 22:06:23 +03:00
|
|
|
}
|
2020-07-30 16:41:53 +03:00
|
|
|
}
|
|
|
|
s.skipWhiteSpace()
|
2020-07-31 11:10:03 +03:00
|
|
|
RETRY:
|
2020-07-30 16:41:53 +03:00
|
|
|
switch s.char() {
|
|
|
|
case ']':
|
2021-02-03 17:50:02 +03:00
|
|
|
slice.cap = capacity
|
2020-07-31 12:52:22 +03:00
|
|
|
slice.len = idx + 1
|
|
|
|
slice.data = data
|
2020-07-30 16:41:53 +03:00
|
|
|
dstCap := idx + 1
|
2020-11-14 21:24:34 +03:00
|
|
|
dst := sliceHeader{
|
|
|
|
data: newArray(d.elemType, dstCap),
|
|
|
|
len: idx + 1,
|
|
|
|
cap: dstCap,
|
2020-07-30 16:41:53 +03:00
|
|
|
}
|
2020-11-14 21:24:34 +03:00
|
|
|
copySlice(d.elemType, dst, sliceHeader{
|
|
|
|
data: slice.data,
|
|
|
|
len: slice.len,
|
|
|
|
cap: slice.cap,
|
2020-07-31 12:52:22 +03:00
|
|
|
})
|
2020-12-05 16:27:33 +03:00
|
|
|
*(*sliceHeader)(p) = dst
|
2020-07-30 16:41:53 +03:00
|
|
|
d.releaseSlice(slice)
|
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() {
|
|
|
|
goto RETRY
|
|
|
|
}
|
2021-02-03 17:50:02 +03:00
|
|
|
slice.cap = capacity
|
2020-07-31 12:52:22 +03:00
|
|
|
slice.data = data
|
2020-07-31 11:10:03 +03:00
|
|
|
d.releaseSlice(slice)
|
|
|
|
goto ERROR
|
2020-07-30 16:41:53 +03:00
|
|
|
default:
|
2021-02-03 17:50:02 +03:00
|
|
|
slice.cap = capacity
|
2020-07-31 12:52:22 +03:00
|
|
|
slice.data = data
|
2020-07-30 16:41:53 +03:00
|
|
|
d.releaseSlice(slice)
|
2020-07-31 11:10:03 +03:00
|
|
|
goto ERROR
|
2020-07-30 16:41:53 +03:00
|
|
|
}
|
2020-08-12 10:54:15 +03:00
|
|
|
s.cursor++
|
2020-07-30 16:41:53 +03:00
|
|
|
}
|
2021-02-09 15:37:18 +03:00
|
|
|
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
2021-02-09 16:20:23 +03:00
|
|
|
return d.errNumber(s.totalOffset())
|
2020-07-31 11:10:03 +03:00
|
|
|
case nul:
|
|
|
|
if s.read() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
goto ERROR
|
2021-02-09 15:37:18 +03:00
|
|
|
default:
|
|
|
|
goto ERROR
|
2020-07-30 16:41:53 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-31 11:10:03 +03:00
|
|
|
ERROR:
|
2020-07-30 16:41:53 +03:00
|
|
|
return errUnexpectedEndOfJSON("slice", s.totalOffset())
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:05:06 +03:00
|
|
|
func (d *sliceDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
|
|
|
|
depth++
|
|
|
|
if depth > maxDecodeNestingDepth {
|
|
|
|
return 0, errExceededMaxDepth(buf[cursor], cursor)
|
|
|
|
}
|
|
|
|
|
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
|
2021-02-16 19:51:42 +03:00
|
|
|
*(*unsafe.Pointer)(p) = nil
|
2020-08-08 07:20:42 +03:00
|
|
|
return cursor, nil
|
2020-04-25 13:55:05 +03:00
|
|
|
case '[':
|
2020-08-12 10:54:15 +03:00
|
|
|
cursor++
|
|
|
|
cursor = skipWhiteSpace(buf, cursor)
|
|
|
|
if buf[cursor] == ']' {
|
2020-11-14 23:27:15 +03:00
|
|
|
**(**sliceHeader)(unsafe.Pointer(&p)) = sliceHeader{
|
2020-11-14 21:24:34 +03:00
|
|
|
data: newArray(d.elemType, 0),
|
|
|
|
len: 0,
|
|
|
|
cap: 0,
|
2020-08-12 10:54:15 +03:00
|
|
|
}
|
|
|
|
cursor++
|
|
|
|
return cursor, nil
|
|
|
|
}
|
2020-04-25 13:55:05 +03:00
|
|
|
idx := 0
|
2020-05-07 15:30:36 +03:00
|
|
|
slice := d.newSlice()
|
2021-02-03 17:50:02 +03:00
|
|
|
capacity := slice.cap
|
2020-05-30 18:11:50 +03:00
|
|
|
data := slice.data
|
2020-04-25 13:55:05 +03:00
|
|
|
for {
|
2021-02-03 17:50:02 +03:00
|
|
|
if capacity <= idx {
|
|
|
|
src := sliceHeader{data: data, len: idx, cap: capacity}
|
|
|
|
capacity *= 2
|
|
|
|
data = newArray(d.elemType, capacity)
|
|
|
|
dst := sliceHeader{data: data, len: idx, cap: capacity}
|
2020-04-25 13:55:05 +03:00
|
|
|
copySlice(d.elemType, dst, src)
|
|
|
|
}
|
2021-03-28 20:28:04 +03:00
|
|
|
ep := unsafe.Pointer(uintptr(data) + uintptr(idx)*d.size)
|
2021-04-30 16:55:08 +03:00
|
|
|
if d.isElemUnmarshalJSONType {
|
|
|
|
receiver := unsafe_New(d.elemType)
|
|
|
|
if d.elemType.Kind() == reflect.Slice {
|
|
|
|
*(*unsafe.Pointer)(ep) = receiver
|
|
|
|
} else {
|
|
|
|
*(*unsafe.Pointer)(ep) = *(*unsafe.Pointer)(receiver)
|
2021-04-29 22:02:06 +03:00
|
|
|
}
|
2021-04-30 16:55:08 +03:00
|
|
|
c, err := d.valueDecoder.decode(buf, cursor, depth, ep)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
cursor = c
|
|
|
|
} else {
|
|
|
|
if d.isElemPointerType {
|
|
|
|
*(*unsafe.Pointer)(ep) = nil // initialize elem pointer
|
|
|
|
}
|
|
|
|
c, err := d.valueDecoder.decode(buf, cursor, depth, ep)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
cursor = c
|
2021-03-28 20:44:49 +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 ']':
|
2021-02-03 17:50:02 +03:00
|
|
|
slice.cap = capacity
|
2020-05-30 18:11:50 +03:00
|
|
|
slice.len = idx + 1
|
|
|
|
slice.data = data
|
2020-05-07 15:30:36 +03:00
|
|
|
dstCap := idx + 1
|
2020-11-14 21:24:34 +03:00
|
|
|
dst := sliceHeader{
|
|
|
|
data: newArray(d.elemType, dstCap),
|
|
|
|
len: idx + 1,
|
|
|
|
cap: dstCap,
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
2020-11-14 21:24:34 +03:00
|
|
|
copySlice(d.elemType, dst, sliceHeader{
|
|
|
|
data: slice.data,
|
|
|
|
len: slice.len,
|
|
|
|
cap: slice.cap,
|
2020-05-30 18:11:50 +03:00
|
|
|
})
|
2020-11-14 23:27:15 +03:00
|
|
|
**(**sliceHeader)(unsafe.Pointer(&p)) = dst
|
2020-05-07 15:30:36 +03:00
|
|
|
d.releaseSlice(slice)
|
2020-05-06 20:37:29 +03:00
|
|
|
cursor++
|
|
|
|
return cursor, nil
|
2020-04-25 13:55:05 +03:00
|
|
|
case ',':
|
|
|
|
idx++
|
|
|
|
default:
|
2021-02-03 17:50:02 +03:00
|
|
|
slice.cap = capacity
|
2020-05-30 18:11:50 +03:00
|
|
|
slice.data = data
|
2020-05-07 15:30:36 +03:00
|
|
|
d.releaseSlice(slice)
|
2020-05-23 06:51:09 +03:00
|
|
|
return 0, errInvalidCharacter(buf[cursor], "slice", cursor)
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
2020-08-12 10:54:15 +03:00
|
|
|
cursor++
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
2021-02-09 16:19:13 +03:00
|
|
|
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
2021-02-09 16:20:23 +03:00
|
|
|
return 0, d.errNumber(cursor)
|
2021-02-09 16:19:13 +03:00
|
|
|
default:
|
|
|
|
goto ERROR
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
|
|
|
}
|
2021-02-09 16:19:13 +03:00
|
|
|
ERROR:
|
2020-05-23 06:51:09 +03:00
|
|
|
return 0, errUnexpectedEndOfJSON("slice", cursor)
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|