2020-04-23 19:39:20 +03:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ptrDecoder struct {
|
2020-11-23 11:16:31 +03:00
|
|
|
dec decoder
|
|
|
|
typ *rtype
|
|
|
|
structName string
|
|
|
|
fieldName string
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
func newPtrDecoder(dec decoder, typ *rtype, structName, fieldName string) *ptrDecoder {
|
|
|
|
return &ptrDecoder{
|
|
|
|
dec: dec,
|
|
|
|
typ: typ,
|
|
|
|
structName: structName,
|
|
|
|
fieldName: fieldName,
|
|
|
|
}
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
|
|
|
|
2020-12-22 15:55:59 +03:00
|
|
|
func (d *ptrDecoder) contentDecoder() decoder {
|
|
|
|
dec, ok := d.dec.(*ptrDecoder)
|
|
|
|
if !ok {
|
|
|
|
return d.dec
|
|
|
|
}
|
|
|
|
return dec.contentDecoder()
|
|
|
|
}
|
|
|
|
|
2021-02-01 16:31:39 +03:00
|
|
|
//nolint:golint
|
2020-04-24 14:23:26 +03:00
|
|
|
//go:linkname unsafe_New reflect.unsafe_New
|
2020-11-19 06:47:42 +03:00
|
|
|
func unsafe_New(*rtype) unsafe.Pointer
|
2020-04-24 14:23:26 +03:00
|
|
|
|
2020-11-19 06:47:42 +03:00
|
|
|
func (d *ptrDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
|
2020-11-27 11:11:53 +03:00
|
|
|
s.skipWhiteSpace()
|
|
|
|
if s.char() == nul {
|
|
|
|
s.read()
|
|
|
|
}
|
|
|
|
if s.char() == 'n' {
|
|
|
|
if err := nullBytes(s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-22 19:29:13 +03:00
|
|
|
*(*unsafe.Pointer)(p) = nil
|
2020-11-27 11:11:53 +03:00
|
|
|
return nil
|
|
|
|
}
|
2020-07-30 16:41:53 +03:00
|
|
|
newptr := unsafe_New(d.typ)
|
2020-11-19 06:47:42 +03:00
|
|
|
*(*unsafe.Pointer)(p) = newptr
|
2020-07-30 16:41:53 +03:00
|
|
|
if err := d.dec.decodeStream(s, newptr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-05 18:54:10 +03:00
|
|
|
func (d *ptrDecoder) decode(buf *sliceHeader, cursor int64, p unsafe.Pointer) (int64, error) {
|
2020-11-27 11:11:53 +03:00
|
|
|
cursor = skipWhiteSpace(buf, cursor)
|
2021-02-05 18:54:10 +03:00
|
|
|
if char(buf.data, cursor) == 'n' {
|
|
|
|
buflen := int64(buf.len)
|
2020-11-27 11:11:53 +03:00
|
|
|
if cursor+3 >= buflen {
|
|
|
|
return 0, errUnexpectedEndOfJSON("null", cursor)
|
|
|
|
}
|
2021-02-05 18:54:10 +03:00
|
|
|
if char(buf.data, cursor+1) != 'u' {
|
|
|
|
return 0, errInvalidCharacter(char(buf.data, cursor+1), "null", cursor)
|
2020-11-27 11:11:53 +03:00
|
|
|
}
|
2021-02-05 18:54:10 +03:00
|
|
|
if char(buf.data, cursor+2) != 'l' {
|
|
|
|
return 0, errInvalidCharacter(char(buf.data, cursor+2), "null", cursor)
|
2020-11-27 11:11:53 +03:00
|
|
|
}
|
2021-02-05 18:54:10 +03:00
|
|
|
if char(buf.data, cursor+3) != 'l' {
|
|
|
|
return 0, errInvalidCharacter(char(buf.data, cursor+3), "null", cursor)
|
2020-11-27 11:11:53 +03:00
|
|
|
}
|
2020-12-22 19:29:13 +03:00
|
|
|
*(*unsafe.Pointer)(p) = nil
|
2020-11-27 11:11:53 +03:00
|
|
|
cursor += 4
|
|
|
|
return cursor, nil
|
|
|
|
}
|
2020-04-24 14:23:26 +03:00
|
|
|
newptr := unsafe_New(d.typ)
|
2020-11-19 06:47:42 +03:00
|
|
|
*(*unsafe.Pointer)(p) = newptr
|
2020-05-06 20:37:29 +03:00
|
|
|
c, err := d.dec.decode(buf, cursor, newptr)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
2020-05-06 20:37:29 +03:00
|
|
|
cursor = c
|
|
|
|
return cursor, nil
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|