| 1 |
package decoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"unsafe" |
| 6 |
|
| 7 |
"github.com/goccy/go-json/internal/errors" |
| 8 |
) |
| 9 |
|
| 10 |
type numberDecoder struct { |
| 11 |
stringDecoder *stringDecoder |
| 12 |
op func(unsafe.Pointer, json.Number) |
| 13 |
structName string |
| 14 |
fieldName string |
| 15 |
} |
| 16 |
|
| 17 |
func newNumberDecoder(structName, fieldName string, op func(unsafe.Pointer, json.Number)) *numberDecoder { |
| 18 |
return &numberDecoder{ |
| 19 |
stringDecoder: newStringDecoder(structName, fieldName), |
| 20 |
op: op, |
| 21 |
structName: structName, |
| 22 |
fieldName: fieldName, |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
func (d *numberDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) { |
| 27 |
buf := ctx.Buf |
| 28 |
cursor = skipWhiteSpace(buf, cursor) |
| 29 |
start := cursor |
| 30 |
switch c := buf[cursor]; { |
| 31 |
case c == '-' || c-'0' <= 9: |
| 32 |
end, err := numberEnd(buf, cursor) |
| 33 |
if err != nil { |
| 34 |
return 0, err |
| 35 |
} |
| 36 |
d.op(p, json.Number(ctx.makeString(buf[cursor:end]))) |
| 37 |
return end, nil |
| 38 |
case c == '"', c == 'n': |
| 39 |
case isOtherValue(c, numberValue): |
| 40 |
return ctx.numberKindError(cursor, depth, jsonNumberType) |
| 41 |
} |
| 42 |
bytes, c, err := d.decodeByte(buf, cursor) |
| 43 |
if err != nil { |
| 44 |
return 0, err |
| 45 |
} |
| 46 |
if bytes == nil { |
| 47 |
// null, which is ignored |
| 48 |
return c, nil |
| 49 |
} |
| 50 |
if !isValidNumber(bytes) { |
| 51 |
return ctx.numberStringError(start, c, jsonNumberType) |
| 52 |
} |
| 53 |
// a number in a string: its bytes end before the quote |
| 54 |
d.op(p, json.Number(ctx.makeString(bytes))) |
| 55 |
return c, nil |
| 56 |
} |
| 57 |
|
| 58 |
// isValidNumber reports whether b is a number by the grammar of the JSON numbers. |
| 59 |
func isValidNumber(b []byte) bool { |
| 60 |
i := 0 |
| 61 |
if i < len(b) && b[i] == '-' { |
| 62 |
i++ |
| 63 |
} |
| 64 |
digits := func() int { |
| 65 |
n := 0 |
| 66 |
for i < len(b) && b[i]-'0' <= 9 { |
| 67 |
i++ |
| 68 |
n++ |
| 69 |
} |
| 70 |
return n |
| 71 |
} |
| 72 |
switch { |
| 73 |
case i < len(b) && b[i] == '0': |
| 74 |
i++ |
| 75 |
case digits() == 0: |
| 76 |
return false |
| 77 |
} |
| 78 |
if i < len(b) && b[i] == '.' { |
| 79 |
i++ |
| 80 |
if digits() == 0 { |
| 81 |
return false |
| 82 |
} |
| 83 |
} |
| 84 |
if i < len(b) && (b[i] == 'e' || b[i] == 'E') { |
| 85 |
i++ |
| 86 |
if i < len(b) && (b[i] == '+' || b[i] == '-') { |
| 87 |
i++ |
| 88 |
} |
| 89 |
if digits() == 0 { |
| 90 |
return false |
| 91 |
} |
| 92 |
} |
| 93 |
return i == len(b) |
| 94 |
} |
| 95 |
|
| 96 |
func (d *numberDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) { |
| 97 |
bytes, c, err := d.decodeByte(ctx.Buf, cursor) |
| 98 |
if err != nil { |
| 99 |
return nil, 0, err |
| 100 |
} |
| 101 |
if bytes == nil { |
| 102 |
return [][]byte{nullbytes}, c, nil |
| 103 |
} |
| 104 |
return [][]byte{bytes}, c, nil |
| 105 |
} |
| 106 |
|
| 107 |
func (d *numberDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error) { |
| 108 |
for { |
| 109 |
switch buf[cursor] { |
| 110 |
case ' ', '\n', '\t', '\r': |
| 111 |
cursor++ |
| 112 |
continue |
| 113 |
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 114 |
start := cursor |
| 115 |
cursor++ |
| 116 |
for floatTable[buf[cursor]] { |
| 117 |
cursor++ |
| 118 |
} |
| 119 |
num := buf[start:cursor] |
| 120 |
return num, cursor, nil |
| 121 |
case 'n': |
| 122 |
if err := validateNull(buf, cursor); err != nil { |
| 123 |
return nil, 0, err |
| 124 |
} |
| 125 |
cursor += 4 |
| 126 |
return nil, cursor, nil |
| 127 |
case '"': |
| 128 |
return d.stringDecoder.decodeByte(buf, cursor) |
| 129 |
case nul: |
| 130 |
return nil, 0, errors.ErrUnexpectedEndOfJSON("json.Number", cursor) |
| 131 |
default: |
| 132 |
return nil, 0, errors.ErrInvalidBeginningOfValue(buf[cursor], cursor+1) |
| 133 |
} |
| 134 |
} |
| 135 |
} |