Refactor json.Number

This commit is contained in:
Masaaki Goshima 2021-03-11 15:29:32 +09:00
parent 66d8dd9c61
commit f57beb9841
4 changed files with 101 additions and 41 deletions

View File

@ -80,7 +80,7 @@ func decodeCompile(typ *rtype, structName, fieldName string, structTypeToDecoder
case reflect.Uint64: case reflect.Uint64:
return decodeCompileUint64(typ, structName, fieldName) return decodeCompileUint64(typ, structName, fieldName)
case reflect.String: case reflect.String:
return decodeCompileString(structName, fieldName) return decodeCompileString(typ, structName, fieldName)
case reflect.Bool: case reflect.Bool:
return decodeCompileBool(structName, fieldName) return decodeCompileBool(structName, fieldName)
case reflect.Float32: case reflect.Float32:
@ -203,7 +203,12 @@ func decodeCompileFloat64(structName, fieldName string) (decoder, error) {
}), nil }), nil
} }
func decodeCompileString(structName, fieldName string) (decoder, error) { func decodeCompileString(typ *rtype, structName, fieldName string) (decoder, error) {
if typ == type2rtype(jsonNumberType) {
return newNumberDecoder(structName, fieldName, func(p unsafe.Pointer, v Number) {
*(*Number)(p) = v
}), nil
}
return newStringDecoder(structName, fieldName), nil return newStringDecoder(structName, fieldName), nil
} }

View File

@ -1,42 +1,118 @@
package json package json
import ( import (
"strconv"
"unsafe" "unsafe"
) )
type numberDecoder struct { type numberDecoder struct {
*floatDecoder stringDecoder *stringDecoder
op func(unsafe.Pointer, Number) op func(unsafe.Pointer, Number)
structName string structName string
fieldName string fieldName string
} }
func newNumberDecoder(structName, fieldName string, op func(unsafe.Pointer, Number)) *numberDecoder { func newNumberDecoder(structName, fieldName string, op func(unsafe.Pointer, Number)) *numberDecoder {
return &numberDecoder{ return &numberDecoder{
floatDecoder: newFloatDecoder(structName, fieldName, nil), stringDecoder: newStringDecoder(structName, fieldName),
op: op, op: op,
structName: structName, structName: structName,
fieldName: fieldName, fieldName: fieldName,
} }
} }
func (d *numberDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error { func (d *numberDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
bytes, err := d.floatDecoder.decodeStreamByte(s) bytes, err := d.decodeStreamByte(s)
if err != nil { if err != nil {
return err return err
} }
if _, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&bytes)), 64); err != nil {
return &SyntaxError{msg: err.Error(), Offset: s.totalOffset()}
}
d.op(p, Number(string(bytes))) d.op(p, Number(string(bytes)))
s.reset() s.reset()
return nil return nil
} }
func (d *numberDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) { func (d *numberDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.floatDecoder.decodeByte(buf, cursor) bytes, c, err := d.decodeByte(buf, cursor)
if err != nil { if err != nil {
return 0, err return 0, err
} }
if _, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&bytes)), 64); err != nil {
return 0, &SyntaxError{msg: err.Error(), Offset: c}
}
cursor = c cursor = c
s := *(*string)(unsafe.Pointer(&bytes)) s := *(*string)(unsafe.Pointer(&bytes))
d.op(p, Number(s)) d.op(p, Number(s))
return cursor, nil return cursor, nil
} }
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:
return nil, errUnexpectedEndOfJSON("json.Number", s.totalOffset())
}
func (d *numberDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error) {
buflen := int64(len(buf))
for ; cursor < buflen; cursor++ {
switch buf[cursor] {
case ' ', '\n', '\t', '\r':
continue
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
start := cursor
cursor++
for ; cursor < buflen; cursor++ {
if floatTable[buf[cursor]] {
continue
}
break
}
num := buf[start:cursor]
return num, cursor, nil
case 'n':
if cursor+3 >= buflen {
return nil, 0, errUnexpectedEndOfJSON("null", cursor)
}
if buf[cursor+1] != 'u' {
return nil, 0, errInvalidCharacter(buf[cursor+1], "null", cursor)
}
if buf[cursor+2] != 'l' {
return nil, 0, errInvalidCharacter(buf[cursor+2], "null", cursor)
}
if buf[cursor+3] != 'l' {
return nil, 0, errInvalidCharacter(buf[cursor+3], "null", cursor)
}
cursor += 4
return nil, cursor, nil
case '"':
return d.stringDecoder.decodeByte(buf, cursor)
default:
return nil, 0, errUnexpectedEndOfJSON("json.Number", cursor)
}
}
return nil, 0, errUnexpectedEndOfJSON("json.Number", cursor)
}

View File

@ -1251,31 +1251,31 @@ var unmarshalTests = []unmarshalTest{
in: `invalid`, // 143 in: `invalid`, // 143
ptr: new(json.Number), ptr: new(json.Number),
err: json.NewSyntaxError( err: json.NewSyntaxError(
`json: invalid character v as null`, `json: json.Number unexpected end of JSON input`,
1, 1,
), ),
}, },
{ {
in: `"invalid"`, // 144 in: `"invalid"`, // 144
ptr: new(json.Number), ptr: new(json.Number),
err: fmt.Errorf(`strconv.ParseFloat: parsing "\"invalid\"": invalid syntax`), err: fmt.Errorf(`strconv.ParseFloat: parsing "invalid": invalid syntax`),
}, },
{ {
in: `{"A":"invalid"}`, // 145 in: `{"A":"invalid"}`, // 145
ptr: new(struct{ A json.Number }), ptr: new(struct{ A json.Number }),
err: fmt.Errorf(`strconv.ParseFloat: parsing "\"invalid\"": invalid syntax`), err: fmt.Errorf(`strconv.ParseFloat: parsing "invalid": invalid syntax`),
}, },
{ {
in: `{"A":"invalid"}`, // 146 in: `{"A":"invalid"}`, // 146
ptr: new(struct { ptr: new(struct {
A json.Number `json:",string"` A json.Number `json:",string"`
}), }),
err: fmt.Errorf(`json: null unexpected end of JSON input`), err: fmt.Errorf(`json: json.Number unexpected end of JSON input`),
}, },
{ {
in: `{"A":"invalid"}`, // 147 in: `{"A":"invalid"}`, // 147
ptr: new(map[string]json.Number), ptr: new(map[string]json.Number),
err: fmt.Errorf(`strconv.ParseFloat: parsing "\"invalid\"": invalid syntax`), err: fmt.Errorf(`strconv.ParseFloat: parsing "invalid": invalid syntax`),
}, },
/* /*
// invalid UTF-8 is coerced to valid UTF-8. // invalid UTF-8 is coerced to valid UTF-8.
@ -1836,6 +1836,7 @@ func TestUnmarshal(t *testing.T) {
// Check round trip also decodes correctly. // Check round trip also decodes correctly.
if tt.err == nil { if tt.err == nil {
//fmt.Println("iface = ", v.Interface(), v.Type())
enc, err := json.Marshal(v.Interface()) enc, err := json.Marshal(v.Interface())
if err != nil { if err != nil {
t.Errorf("#%d: error re-marshaling: %v", i, err) t.Errorf("#%d: error re-marshaling: %v", i, err)

26
json.go
View File

@ -2,8 +2,8 @@ package json
import ( import (
"bytes" "bytes"
"encoding/json"
"errors" "errors"
"strconv"
) )
// Marshaler is the interface implemented by types that // Marshaler is the interface implemented by types that
@ -281,29 +281,7 @@ func UnmarshalNoEscape(data []byte, v interface{}) error {
type Token interface{} type Token interface{}
// A Number represents a JSON number literal. // A Number represents a JSON number literal.
type Number string type Number = json.Number
// String returns the literal text of the number.
func (n Number) String() string { return string(n) }
// Float64 returns the number as a float64.
func (n Number) Float64() (float64, error) {
return strconv.ParseFloat(string(n), 64)
}
// Int64 returns the number as an int64.
func (n Number) Int64() (int64, error) {
return strconv.ParseInt(string(n), 10, 64)
}
func (n *Number) UnmarshalJSON(b []byte) error {
s := string(b)
if _, err := strconv.ParseFloat(s, 64); err != nil {
return &SyntaxError{msg: err.Error()}
}
*n = Number(s)
return nil
}
// RawMessage is a raw encoded JSON value. // RawMessage is a raw encoded JSON value.
// It implements Marshaler and Unmarshaler and can // It implements Marshaler and Unmarshaler and can