go-json/decode_unmarshal_json.go

70 lines
1.4 KiB
Go
Raw Permalink Normal View History

2020-05-08 14:22:57 +03:00
package json
import (
"unsafe"
)
type unmarshalJSONDecoder struct {
2020-12-15 06:29:19 +03:00
typ *rtype
structName string
fieldName string
2020-05-08 14:22:57 +03:00
}
func newUnmarshalJSONDecoder(typ *rtype, structName, fieldName string) *unmarshalJSONDecoder {
return &unmarshalJSONDecoder{
typ: typ,
structName: structName,
fieldName: fieldName,
}
}
2020-11-27 11:11:53 +03:00
func (d *unmarshalJSONDecoder) annotateError(cursor int64, err error) {
switch e := err.(type) {
case *UnmarshalTypeError:
e.Struct = d.structName
e.Field = d.fieldName
case *SyntaxError:
e.Offset = cursor
}
2020-05-08 14:22:57 +03:00
}
2020-11-19 06:47:42 +03:00
func (d *unmarshalJSONDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
2020-07-30 16:41:53 +03:00
s.skipWhiteSpace()
start := s.cursor
if err := s.skipValue(); err != nil {
return err
}
src := s.buf[start:s.cursor]
2020-12-05 16:27:33 +03:00
dst := make([]byte, len(src))
copy(dst, src)
2020-12-07 04:44:24 +03:00
v := *(*interface{})(unsafe.Pointer(&interfaceHeader{
typ: d.typ,
ptr: p,
}))
if err := v.(Unmarshaler).UnmarshalJSON(dst); err != nil {
d.annotateError(s.cursor, err)
return err
2020-07-30 16:41:53 +03:00
}
return nil
}
2020-11-19 06:47:42 +03:00
func (d *unmarshalJSONDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
2020-05-08 14:22:57 +03:00
cursor = skipWhiteSpace(buf, cursor)
start := cursor
end, err := skipValue(buf, cursor)
if err != nil {
return 0, err
}
src := buf[start:end]
2020-12-15 06:29:19 +03:00
v := *(*interface{})(unsafe.Pointer(&interfaceHeader{
typ: d.typ,
ptr: p,
}))
if err := v.(Unmarshaler).UnmarshalJSON(src); err != nil {
d.annotateError(cursor, err)
return 0, err
2020-05-08 14:22:57 +03:00
}
return end, nil
}