2020-11-21 20:47:18 +03:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
type bytesDecoder struct {
|
2020-12-24 08:26:18 +03:00
|
|
|
typ *rtype
|
|
|
|
sliceDecoder decoder
|
|
|
|
structName string
|
|
|
|
fieldName string
|
2020-11-23 11:16:31 +03:00
|
|
|
}
|
2020-11-21 20:47:18 +03:00
|
|
|
|
2020-12-24 08:26:18 +03:00
|
|
|
func byteUnmarshalerSliceDecoder(typ *rtype, structName string, fieldName string) decoder {
|
|
|
|
var unmarshalDecoder decoder
|
|
|
|
switch {
|
|
|
|
case rtype_ptrTo(typ).Implements(unmarshalJSONType):
|
|
|
|
unmarshalDecoder = newUnmarshalJSONDecoder(rtype_ptrTo(typ), structName, fieldName)
|
|
|
|
case rtype_ptrTo(typ).Implements(unmarshalTextType):
|
|
|
|
unmarshalDecoder = newUnmarshalTextDecoder(rtype_ptrTo(typ), structName, fieldName)
|
|
|
|
}
|
|
|
|
if unmarshalDecoder == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return newSliceDecoder(unmarshalDecoder, typ, 1, structName, fieldName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBytesDecoder(typ *rtype, structName string, fieldName string) *bytesDecoder {
|
|
|
|
return &bytesDecoder{
|
|
|
|
typ: typ,
|
|
|
|
sliceDecoder: byteUnmarshalerSliceDecoder(typ, structName, fieldName),
|
|
|
|
structName: structName,
|
|
|
|
fieldName: fieldName,
|
|
|
|
}
|
2020-11-21 20:47:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *bytesDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
|
2020-12-24 08:26:18 +03:00
|
|
|
bytes, err := d.decodeStreamBinary(s, p)
|
2020-11-21 20:47:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-05 16:27:33 +03:00
|
|
|
if bytes == nil {
|
|
|
|
s.reset()
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-21 20:47:18 +03:00
|
|
|
decodedLen := base64.StdEncoding.DecodedLen(len(bytes))
|
|
|
|
buf := make([]byte, decodedLen)
|
|
|
|
if _, err := base64.StdEncoding.Decode(buf, bytes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*(*[]byte)(p) = buf
|
2020-12-05 16:27:33 +03:00
|
|
|
s.reset()
|
2020-11-21 20:47:18 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-05 18:54:10 +03:00
|
|
|
func (d *bytesDecoder) decode(buf *sliceHeader, cursor int64, p unsafe.Pointer) (int64, error) {
|
2020-12-24 08:26:18 +03:00
|
|
|
bytes, c, err := d.decodeBinary(buf, cursor, p)
|
2020-11-21 20:47:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-12-24 08:26:18 +03:00
|
|
|
if bytes == nil {
|
|
|
|
return c, nil
|
|
|
|
}
|
2020-11-21 20:47:18 +03:00
|
|
|
cursor = c
|
|
|
|
decodedLen := base64.StdEncoding.DecodedLen(len(bytes))
|
|
|
|
b := make([]byte, decodedLen)
|
2020-12-24 13:36:49 +03:00
|
|
|
n, err := base64.StdEncoding.Decode(b, bytes)
|
|
|
|
if err != nil {
|
2020-11-21 20:47:18 +03:00
|
|
|
return 0, err
|
|
|
|
}
|
2020-12-24 13:36:49 +03:00
|
|
|
*(*[]byte)(p) = b[:n]
|
2020-11-21 20:47:18 +03:00
|
|
|
return cursor, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func binaryBytes(s *stream) ([]byte, error) {
|
|
|
|
s.cursor++
|
|
|
|
start := s.cursor
|
|
|
|
for {
|
|
|
|
switch s.char() {
|
|
|
|
case '"':
|
|
|
|
literal := s.buf[start:s.cursor]
|
|
|
|
s.cursor++
|
|
|
|
return literal, nil
|
|
|
|
case nul:
|
|
|
|
if s.read() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
goto ERROR
|
|
|
|
}
|
|
|
|
s.cursor++
|
|
|
|
}
|
|
|
|
ERROR:
|
|
|
|
return nil, errUnexpectedEndOfJSON("[]byte", s.totalOffset())
|
|
|
|
}
|
|
|
|
|
2020-12-24 08:26:18 +03:00
|
|
|
func (d *bytesDecoder) decodeStreamBinary(s *stream, p unsafe.Pointer) ([]byte, error) {
|
2020-11-21 20:47:18 +03:00
|
|
|
for {
|
|
|
|
switch s.char() {
|
|
|
|
case ' ', '\n', '\t', '\r':
|
|
|
|
s.cursor++
|
|
|
|
continue
|
|
|
|
case '"':
|
|
|
|
return binaryBytes(s)
|
|
|
|
case 'n':
|
|
|
|
if err := nullBytes(s); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-05 16:27:33 +03:00
|
|
|
return nil, nil
|
2020-12-24 08:26:18 +03:00
|
|
|
case '[':
|
|
|
|
if d.sliceDecoder == nil {
|
|
|
|
return nil, &UnmarshalTypeError{
|
|
|
|
Type: rtype2type(d.typ),
|
|
|
|
Offset: s.totalOffset(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := d.sliceDecoder.decodeStream(s, p); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
2020-11-21 20:47:18 +03:00
|
|
|
case nul:
|
|
|
|
if s.read() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return nil, errNotAtBeginningOfValue(s.totalOffset())
|
|
|
|
}
|
|
|
|
|
2021-02-05 18:54:10 +03:00
|
|
|
func (d *bytesDecoder) decodeBinary(buf *sliceHeader, cursor int64, p unsafe.Pointer) ([]byte, int64, error) {
|
2020-11-21 20:47:18 +03:00
|
|
|
for {
|
2021-02-05 18:54:10 +03:00
|
|
|
switch char(buf.data, cursor) {
|
2020-11-21 20:47:18 +03:00
|
|
|
case ' ', '\n', '\t', '\r':
|
|
|
|
cursor++
|
|
|
|
case '"':
|
|
|
|
cursor++
|
|
|
|
start := cursor
|
|
|
|
for {
|
2021-02-05 18:54:10 +03:00
|
|
|
switch char(buf.data, cursor) {
|
2020-11-21 20:47:18 +03:00
|
|
|
case '"':
|
2021-02-05 18:54:10 +03:00
|
|
|
literal := (*(*[]byte)(unsafe.Pointer(buf)))[start:cursor]
|
2020-11-21 20:47:18 +03:00
|
|
|
cursor++
|
|
|
|
return literal, cursor, nil
|
|
|
|
case nul:
|
|
|
|
return nil, 0, errUnexpectedEndOfJSON("[]byte", cursor)
|
|
|
|
}
|
|
|
|
cursor++
|
|
|
|
}
|
2020-12-24 08:26:18 +03:00
|
|
|
case '[':
|
|
|
|
if d.sliceDecoder == nil {
|
|
|
|
return nil, 0, &UnmarshalTypeError{
|
|
|
|
Type: rtype2type(d.typ),
|
|
|
|
Offset: cursor,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c, err := d.sliceDecoder.decode(buf, cursor, p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return nil, c, nil
|
2020-11-21 20:47:18 +03:00
|
|
|
case 'n':
|
2021-02-05 18:54:10 +03:00
|
|
|
buflen := int64(buf.len)
|
2020-11-21 20:47:18 +03:00
|
|
|
if cursor+3 >= buflen {
|
|
|
|
return nil, 0, errUnexpectedEndOfJSON("null", cursor)
|
|
|
|
}
|
2021-02-05 18:54:10 +03:00
|
|
|
if char(buf.data, cursor+1) != 'u' {
|
|
|
|
return nil, 0, errInvalidCharacter(char(buf.data, cursor+1), "null", cursor)
|
2020-11-21 20:47:18 +03:00
|
|
|
}
|
2021-02-05 18:54:10 +03:00
|
|
|
if char(buf.data, cursor+2) != 'l' {
|
|
|
|
return nil, 0, errInvalidCharacter(char(buf.data, cursor+2), "null", cursor)
|
2020-11-21 20:47:18 +03:00
|
|
|
}
|
2021-02-05 18:54:10 +03:00
|
|
|
if char(buf.data, cursor+3) != 'l' {
|
|
|
|
return nil, 0, errInvalidCharacter(char(buf.data, cursor+3), "null", cursor)
|
2020-11-21 20:47:18 +03:00
|
|
|
}
|
|
|
|
cursor += 4
|
|
|
|
return []byte{}, cursor, nil
|
|
|
|
default:
|
|
|
|
goto ERROR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ERROR:
|
|
|
|
return nil, 0, errNotAtBeginningOfValue(cursor)
|
|
|
|
}
|