go-json/internal/decoder/unmarshal_text.go

286 lines
5.7 KiB
Go
Raw Permalink Normal View History

2021-06-03 12:49:01 +03:00
package decoder
2020-05-08 14:25:49 +03:00
import (
2021-02-16 19:51:42 +03:00
"bytes"
2020-05-08 14:25:49 +03:00
"encoding"
2022-11-28 21:55:56 +03:00
"fmt"
"unicode"
"unicode/utf16"
"unicode/utf8"
2020-05-08 14:25:49 +03:00
"unsafe"
2021-06-03 12:49:01 +03:00
"github.com/goccy/go-json/internal/errors"
"github.com/goccy/go-json/internal/runtime"
2020-05-08 14:25:49 +03:00
)
type unmarshalTextDecoder struct {
2021-06-03 12:49:01 +03:00
typ *runtime.Type
structName string
fieldName string
2020-05-08 14:25:49 +03:00
}
2021-06-03 12:49:01 +03:00
func newUnmarshalTextDecoder(typ *runtime.Type, structName, fieldName string) *unmarshalTextDecoder {
return &unmarshalTextDecoder{
typ: typ,
structName: structName,
fieldName: fieldName,
}
}
2020-11-27 11:11:53 +03:00
func (d *unmarshalTextDecoder) annotateError(cursor int64, err error) {
switch e := err.(type) {
2021-06-03 12:49:01 +03:00
case *errors.UnmarshalTypeError:
2020-11-27 11:11:53 +03:00
e.Struct = d.structName
e.Field = d.fieldName
2021-06-03 12:49:01 +03:00
case *errors.SyntaxError:
2020-11-27 11:11:53 +03:00
e.Offset = cursor
}
2020-05-08 14:25:49 +03:00
}
2021-02-16 19:51:42 +03:00
var (
nullbytes = []byte(`null`)
)
2021-06-03 12:49:01 +03:00
func (d *unmarshalTextDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error {
2020-07-30 16:41:53 +03:00
s.skipWhiteSpace()
start := s.cursor
if err := s.skipValue(depth); err != nil {
2020-07-30 16:41:53 +03:00
return err
}
src := s.buf[start:s.cursor]
2021-02-18 10:42:38 +03:00
if len(src) > 0 {
switch src[0] {
case '[':
2021-06-03 12:49:01 +03:00
return &errors.UnmarshalTypeError{
2021-02-18 10:42:38 +03:00
Value: "array",
2021-06-03 12:49:01 +03:00
Type: runtime.RType2Type(d.typ),
2021-02-18 10:42:38 +03:00
Offset: s.totalOffset(),
}
case '{':
2021-06-03 12:49:01 +03:00
return &errors.UnmarshalTypeError{
2021-02-18 10:42:38 +03:00
Value: "object",
2021-06-03 12:49:01 +03:00
Type: runtime.RType2Type(d.typ),
2021-02-18 10:42:38 +03:00
Offset: s.totalOffset(),
}
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
2021-06-03 12:49:01 +03:00
return &errors.UnmarshalTypeError{
2021-02-18 10:42:38 +03:00
Value: "number",
2021-06-03 12:49:01 +03:00
Type: runtime.RType2Type(d.typ),
2021-02-18 10:42:38 +03:00
Offset: s.totalOffset(),
}
case 'n':
if bytes.Equal(src, nullbytes) {
*(*unsafe.Pointer)(p) = nil
return nil
}
2021-02-16 19:51:42 +03:00
}
2020-12-24 12:45:26 +03:00
}
2020-12-05 16:27:33 +03:00
dst := make([]byte, len(src))
copy(dst, src)
if b, ok := unquoteBytes(dst); ok {
dst = b
}
2021-03-13 08:12:31 +03:00
v := *(*interface{})(unsafe.Pointer(&emptyInterface{
2020-07-30 16:41:53 +03:00
typ: d.typ,
2020-12-07 04:44:24 +03:00
ptr: p,
2020-07-30 16:41:53 +03:00
}))
2020-12-05 16:27:33 +03:00
if err := v.(encoding.TextUnmarshaler).UnmarshalText(dst); err != nil {
2020-11-27 11:11:53 +03:00
d.annotateError(s.cursor, err)
2020-07-30 16:41:53 +03:00
return err
}
return nil
}
2021-06-04 19:08:27 +03:00
func (d *unmarshalTextDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
buf := ctx.Buf
2020-05-08 14:25:49 +03:00
cursor = skipWhiteSpace(buf, cursor)
start := cursor
end, err := skipValue(buf, cursor, depth)
2020-05-08 14:25:49 +03:00
if err != nil {
return 0, err
}
src := buf[start:end]
2021-02-18 10:42:38 +03:00
if len(src) > 0 {
switch src[0] {
case '[':
2021-06-03 12:49:01 +03:00
return 0, &errors.UnmarshalTypeError{
2021-02-18 10:42:38 +03:00
Value: "array",
2021-06-03 12:49:01 +03:00
Type: runtime.RType2Type(d.typ),
2021-02-18 10:42:38 +03:00
Offset: start,
}
case '{':
2021-06-03 12:49:01 +03:00
return 0, &errors.UnmarshalTypeError{
2021-02-18 10:42:38 +03:00
Value: "object",
2021-06-03 12:49:01 +03:00
Type: runtime.RType2Type(d.typ),
2021-02-18 10:42:38 +03:00
Offset: start,
}
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
2021-06-03 12:49:01 +03:00
return 0, &errors.UnmarshalTypeError{
2021-02-18 10:42:38 +03:00
Value: "number",
2021-06-03 12:49:01 +03:00
Type: runtime.RType2Type(d.typ),
2021-02-18 10:42:38 +03:00
Offset: start,
}
case 'n':
if bytes.Equal(src, nullbytes) {
*(*unsafe.Pointer)(p) = nil
return end, nil
}
}
2021-02-16 19:51:42 +03:00
}
if s, ok := unquoteBytes(src); ok {
src = s
}
2021-03-13 08:12:31 +03:00
v := *(*interface{})(unsafe.Pointer(&emptyInterface{
2020-05-08 14:25:49 +03:00
typ: d.typ,
ptr: *(*unsafe.Pointer)(unsafe.Pointer(&p)),
2020-05-08 14:25:49 +03:00
}))
if err := v.(encoding.TextUnmarshaler).UnmarshalText(src); err != nil {
2020-11-27 11:11:53 +03:00
d.annotateError(cursor, err)
2020-05-08 14:25:49 +03:00
return 0, err
}
return end, nil
}
2022-11-28 21:55:56 +03:00
func (d *unmarshalTextDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) {
return nil, 0, fmt.Errorf("json: unmarshal text decoder does not support decode path")
}
func unquoteBytes(s []byte) (t []byte, ok bool) { //nolint: nonamedreturns
length := len(s)
if length < 2 || s[0] != '"' || s[length-1] != '"' {
return
}
s = s[1 : length-1]
length -= 2
// Check for unusual characters. If there are none,
// then no unquoting is needed, so return a slice of the
// original bytes.
r := 0
for r < length {
c := s[r]
if c == '\\' || c == '"' || c < ' ' {
break
}
if c < utf8.RuneSelf {
r++
continue
}
rr, size := utf8.DecodeRune(s[r:])
if rr == utf8.RuneError && size == 1 {
break
}
r += size
}
if r == length {
return s, true
}
b := make([]byte, length+2*utf8.UTFMax)
w := copy(b, s[0:r])
for r < length {
// Out of room? Can only happen if s is full of
// malformed UTF-8 and we're replacing each
// byte with RuneError.
if w >= len(b)-2*utf8.UTFMax {
nb := make([]byte, (len(b)+utf8.UTFMax)*2)
copy(nb, b[0:w])
b = nb
}
switch c := s[r]; {
case c == '\\':
r++
if r >= length {
return
}
switch s[r] {
default:
return
case '"', '\\', '/', '\'':
b[w] = s[r]
r++
w++
case 'b':
b[w] = '\b'
r++
w++
case 'f':
b[w] = '\f'
r++
w++
case 'n':
b[w] = '\n'
r++
w++
case 'r':
b[w] = '\r'
r++
w++
case 't':
b[w] = '\t'
r++
w++
case 'u':
r--
rr := getu4(s[r:])
if rr < 0 {
return
}
r += 6
if utf16.IsSurrogate(rr) {
rr1 := getu4(s[r:])
if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar {
// A valid pair; consume.
r += 6
w += utf8.EncodeRune(b[w:], dec)
break
}
// Invalid surrogate; fall back to replacement rune.
rr = unicode.ReplacementChar
}
w += utf8.EncodeRune(b[w:], rr)
}
// Quote, control characters are invalid.
case c == '"', c < ' ':
return
// ASCII
case c < utf8.RuneSelf:
b[w] = c
r++
w++
// Coerce to well-formed UTF-8.
default:
rr, size := utf8.DecodeRune(s[r:])
r += size
w += utf8.EncodeRune(b[w:], rr)
}
}
return b[0:w], true
}
func getu4(s []byte) rune {
if len(s) < 6 || s[0] != '\\' || s[1] != 'u' {
return -1
}
var r rune
for _, c := range s[2:6] {
switch {
case '0' <= c && c <= '9':
c = c - '0'
case 'a' <= c && c <= 'f':
c = c - 'a' + 10
case 'A' <= c && c <= 'F':
c = c - 'A' + 10
default:
return -1
}
r = r*16 + rune(c)
}
return r
}