go-json/internal/decoder/number.go

109 lines
2.5 KiB
Go
Raw Normal View History

2021-06-03 12:49:01 +03:00
package decoder
2020-08-11 12:04:32 +03:00
import (
2021-06-03 12:49:01 +03:00
"encoding/json"
"strconv"
2020-08-11 12:04:32 +03:00
"unsafe"
2021-06-03 12:49:01 +03:00
"github.com/goccy/go-json/internal/errors"
2020-08-11 12:04:32 +03:00
)
type numberDecoder struct {
stringDecoder *stringDecoder
2021-06-03 12:49:01 +03:00
op func(unsafe.Pointer, json.Number)
structName string
fieldName string
2020-08-11 12:04:32 +03:00
}
2021-06-03 12:49:01 +03:00
func newNumberDecoder(structName, fieldName string, op func(unsafe.Pointer, json.Number)) *numberDecoder {
2020-08-11 12:04:32 +03:00
return &numberDecoder{
stringDecoder: newStringDecoder(structName, fieldName),
op: op,
structName: structName,
fieldName: fieldName,
2020-08-11 12:04:32 +03:00
}
}
2021-06-03 12:49:01 +03:00
func (d *numberDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error {
bytes, err := d.decodeStreamByte(s)
2020-08-11 12:04:32 +03:00
if err != nil {
return err
}
if _, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&bytes)), 64); err != nil {
2021-06-03 12:49:01 +03:00
return errors.ErrSyntax(err.Error(), s.totalOffset())
}
2021-06-03 12:49:01 +03:00
d.op(p, json.Number(string(bytes)))
2020-12-05 16:27:33 +03:00
s.reset()
2020-08-11 12:04:32 +03:00
return nil
}
2021-06-04 19:08:27 +03:00
func (d *numberDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.decodeByte(ctx.Buf, cursor)
2020-08-11 12:04:32 +03:00
if err != nil {
return 0, err
}
if _, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&bytes)), 64); err != nil {
2021-06-03 12:49:01 +03:00
return 0, errors.ErrSyntax(err.Error(), c)
}
2020-08-11 12:04:32 +03:00
cursor = c
s := *(*string)(unsafe.Pointer(&bytes))
2021-06-03 12:49:01 +03:00
d.op(p, json.Number(s))
2020-08-11 12:04:32 +03:00
return cursor, nil
}
2021-06-03 12:49:01 +03:00
func (d *numberDecoder) decodeStreamByte(s *Stream) ([]byte, error) {
for {
switch s.char() {
case ' ', '\n', '\t', '\r':
s.cursor++
continue
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return floatBytes(s), nil
case 'n':
if err := nullBytes(s); err != nil {
return nil, err
}
return nil, nil
case '"':
return d.stringDecoder.decodeStreamByte(s)
case nul:
if s.read() {
continue
}
goto ERROR
default:
goto ERROR
}
}
ERROR:
2021-06-03 12:49:01 +03:00
return nil, errors.ErrUnexpectedEndOfJSON("json.Number", s.totalOffset())
}
func (d *numberDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error) {
2021-05-08 21:05:36 +03:00
for {
switch buf[cursor] {
case ' ', '\n', '\t', '\r':
2021-05-08 21:05:36 +03:00
cursor++
continue
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
start := cursor
cursor++
2021-05-08 21:05:36 +03:00
for floatTable[buf[cursor]] {
cursor++
}
num := buf[start:cursor]
return num, cursor, nil
case 'n':
2021-05-08 21:05:36 +03:00
if err := validateNull(buf, cursor); err != nil {
return nil, 0, err
}
cursor += 4
return nil, cursor, nil
case '"':
return d.stringDecoder.decodeByte(buf, cursor)
default:
2021-06-03 12:49:01 +03:00
return nil, 0, errors.ErrUnexpectedEndOfJSON("json.Number", cursor)
}
}
}