go-json/decode_map.go

152 lines
3.5 KiB
Go
Raw Permalink Normal View History

2020-04-25 16:48:16 +03:00
package json
import (
"unsafe"
)
type mapDecoder struct {
mapType *rtype
2020-12-22 15:55:59 +03:00
keyType *rtype
valueType *rtype
2020-04-25 16:48:16 +03:00
keyDecoder decoder
valueDecoder decoder
structName string
fieldName string
2020-04-25 16:48:16 +03:00
}
2020-12-22 15:55:59 +03:00
func newMapDecoder(mapType *rtype, keyType *rtype, keyDec decoder, valueType *rtype, valueDec decoder, structName, fieldName string) *mapDecoder {
2020-04-25 16:48:16 +03:00
return &mapDecoder{
mapType: mapType,
keyDecoder: keyDec,
2020-12-22 15:55:59 +03:00
keyType: keyType,
valueType: valueType,
2020-04-25 16:48:16 +03:00
valueDecoder: valueDec,
structName: structName,
fieldName: fieldName,
2020-04-25 16:48:16 +03:00
}
}
//go:linkname makemap reflect.makemap
func makemap(*rtype, int) unsafe.Pointer
//go:linkname mapassign reflect.mapassign
//go:noescape
func mapassign(t *rtype, m unsafe.Pointer, key, val unsafe.Pointer)
2020-11-19 06:47:42 +03:00
func (d *mapDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
2020-07-30 16:41:53 +03:00
s.skipWhiteSpace()
2020-08-08 07:20:42 +03:00
switch s.char() {
case 'n':
if err := nullBytes(s); err != nil {
return err
}
return nil
case '{':
default:
2020-07-30 16:41:53 +03:00
return errExpected("{ character for map value", s.totalOffset())
}
2020-08-12 10:54:15 +03:00
s.skipWhiteSpace()
2020-07-30 16:41:53 +03:00
mapValue := makemap(d.mapType, 0)
2020-08-12 10:54:15 +03:00
if s.buf[s.cursor+1] == '}' {
2020-12-05 16:27:33 +03:00
*(*unsafe.Pointer)(p) = mapValue
s.cursor += 2
2020-08-12 10:54:15 +03:00
return nil
}
2020-07-31 11:10:03 +03:00
for {
s.cursor++
2020-12-22 15:55:59 +03:00
k := unsafe_New(d.keyType)
if err := d.keyDecoder.decodeStream(s, k); err != nil {
2020-07-30 16:41:53 +03:00
return err
}
s.skipWhiteSpace()
2020-07-31 11:10:03 +03:00
if s.char() == nul {
s.read()
}
2020-07-30 16:41:53 +03:00
if s.char() != ':' {
return errExpected("colon after object key", s.totalOffset())
}
2020-07-31 11:10:03 +03:00
s.cursor++
2020-12-22 15:55:59 +03:00
v := unsafe_New(d.valueType)
if err := d.valueDecoder.decodeStream(s, v); err != nil {
2020-07-30 16:41:53 +03:00
return err
}
2020-12-22 15:55:59 +03:00
mapassign(d.mapType, mapValue, k, v)
2020-07-30 16:41:53 +03:00
s.skipWhiteSpace()
2020-07-31 11:10:03 +03:00
if s.char() == nul {
s.read()
}
2020-07-30 16:41:53 +03:00
if s.char() == '}' {
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue
2020-08-08 07:43:24 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
return nil
}
if s.char() != ',' {
2020-08-14 10:00:29 +03:00
return errExpected("comma after object value", s.totalOffset())
2020-07-30 16:41:53 +03:00
}
}
}
2020-11-19 06:47:42 +03:00
func (d *mapDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
2020-05-06 20:37:29 +03:00
cursor = skipWhiteSpace(buf, cursor)
2020-05-23 06:51:09 +03:00
buflen := int64(len(buf))
2020-04-25 16:48:16 +03:00
if buflen < 2 {
2020-05-23 06:51:09 +03:00
return 0, errExpected("{} for map", cursor)
2020-04-25 16:48:16 +03:00
}
2020-08-08 07:20:42 +03:00
switch buf[cursor] {
case 'n':
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
return cursor, nil
case '{':
default:
2020-05-23 06:51:09 +03:00
return 0, errExpected("{ character for map value", cursor)
2020-04-25 16:48:16 +03:00
}
cursor++
2020-08-12 10:54:15 +03:00
cursor = skipWhiteSpace(buf, cursor)
2020-04-25 16:48:16 +03:00
mapValue := makemap(d.mapType, 0)
2020-08-12 10:54:15 +03:00
if buf[cursor] == '}' {
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue
2020-08-12 10:54:15 +03:00
cursor++
return cursor, nil
}
2021-02-15 12:45:41 +03:00
for {
k := unsafe_New(d.keyType)
keyCursor, err := d.keyDecoder.decode(buf, cursor, k)
2020-05-06 20:37:29 +03:00
if err != nil {
return 0, err
2020-04-25 16:48:16 +03:00
}
2021-02-15 12:45:41 +03:00
cursor = skipWhiteSpace(buf, keyCursor)
2020-04-25 16:48:16 +03:00
if buf[cursor] != ':' {
2020-05-23 06:51:09 +03:00
return 0, errExpected("colon after object key", cursor)
2020-04-25 16:48:16 +03:00
}
cursor++
2021-02-15 12:45:41 +03:00
v := unsafe_New(d.valueType)
valueCursor, err := d.valueDecoder.decode(buf, cursor, v)
2020-05-06 20:37:29 +03:00
if err != nil {
return 0, err
2020-04-25 16:48:16 +03:00
}
2021-02-15 12:45:41 +03:00
mapassign(d.mapType, mapValue, k, v)
2020-05-06 20:37:29 +03:00
cursor = skipWhiteSpace(buf, valueCursor)
2020-04-25 16:48:16 +03:00
if buf[cursor] == '}' {
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue
2020-08-08 07:43:24 +03:00
cursor++
2020-05-06 20:37:29 +03:00
return cursor, nil
2020-04-25 16:48:16 +03:00
}
if buf[cursor] != ',' {
2020-08-14 10:00:29 +03:00
return 0, errExpected("comma after object value", cursor)
2020-04-25 16:48:16 +03:00
}
2021-02-15 12:45:41 +03:00
cursor++
2020-04-25 16:48:16 +03:00
}
}