go-json/decode_map.go

166 lines
4.0 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-05-23 06:51:09 +03:00
func (d *mapDecoder) setKey(buf []byte, cursor int64, key interface{}) (int64, error) {
2020-04-25 16:48:16 +03:00
header := (*interfaceHeader)(unsafe.Pointer(&key))
2020-11-19 06:47:42 +03:00
return d.keyDecoder.decode(buf, cursor, header.ptr)
2020-04-25 16:48:16 +03:00
}
2020-05-23 06:51:09 +03:00
func (d *mapDecoder) setValue(buf []byte, cursor int64, key interface{}) (int64, error) {
2020-04-25 16:48:16 +03:00
header := (*interfaceHeader)(unsafe.Pointer(&key))
2020-11-19 06:47:42 +03:00
return d.valueDecoder.decode(buf, cursor, header.ptr)
2020-04-25 16:48:16 +03:00
}
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
}
2020-04-25 16:48:16 +03:00
for ; cursor < buflen; cursor++ {
var key interface{}
2020-05-06 20:37:29 +03:00
keyCursor, err := d.setKey(buf, cursor, &key)
if err != nil {
return 0, err
2020-04-25 16:48:16 +03:00
}
2020-05-06 20:37:29 +03:00
cursor = keyCursor
cursor = skipWhiteSpace(buf, cursor)
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++
if cursor >= buflen {
2020-05-23 06:51:09 +03:00
return 0, errUnexpectedEndOfJSON("map", cursor)
2020-04-25 16:48:16 +03:00
}
var value interface{}
2020-05-06 20:37:29 +03:00
valueCursor, err := d.setValue(buf, cursor, &value)
if err != nil {
return 0, err
2020-04-25 16:48:16 +03:00
}
mapassign(d.mapType, mapValue, unsafe.Pointer(&key), unsafe.Pointer(&value))
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
}
}
2020-05-06 20:37:29 +03:00
return cursor, nil
2020-04-25 16:48:16 +03:00
}