Fix syntax error

This commit is contained in:
Masaaki Goshima 2020-11-27 17:11:53 +09:00
parent 47b7f4a5a2
commit 7421d82c43
5 changed files with 52 additions and 17 deletions

View File

@ -24,6 +24,16 @@ func newPtrDecoder(dec decoder, typ *rtype, structName, fieldName string) *ptrDe
func unsafe_New(*rtype) unsafe.Pointer
func (d *ptrDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
s.skipWhiteSpace()
if s.char() == nul {
s.read()
}
if s.char() == 'n' {
if err := nullBytes(s); err != nil {
return err
}
return nil
}
newptr := unsafe_New(d.typ)
*(*unsafe.Pointer)(p) = newptr
if err := d.dec.decodeStream(s, newptr); err != nil {
@ -33,6 +43,24 @@ func (d *ptrDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
}
func (d *ptrDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
cursor = skipWhiteSpace(buf, cursor)
if buf[cursor] == 'n' {
buflen := int64(len(buf))
if cursor+3 >= buflen {
return 0, errUnexpectedEndOfJSON("null", cursor)
}
if buf[cursor+1] != 'u' {
return 0, errInvalidCharacter(buf[cursor+1], "null", cursor)
}
if buf[cursor+2] != 'l' {
return 0, errInvalidCharacter(buf[cursor+2], "null", cursor)
}
if buf[cursor+3] != 'l' {
return 0, errInvalidCharacter(buf[cursor+3], "null", cursor)
}
cursor += 4
return cursor, nil
}
newptr := unsafe_New(d.typ)
*(*unsafe.Pointer)(p) = newptr
c, err := d.dec.decode(buf, cursor, newptr)

View File

@ -129,6 +129,9 @@ func (s *stream) skipValue() error {
}
case ']':
bracketCount--
if braceCount == 0 && bracketCount == 0 {
return nil
}
case ',':
if bracketCount == 0 && braceCount == 0 {
return nil

View File

@ -19,11 +19,13 @@ func newUnmarshalJSONDecoder(typ *rtype, structName, fieldName string) *unmarsha
}
}
func (d *unmarshalJSONDecoder) annotateError(err error) {
ut, ok := err.(*UnmarshalTypeError)
if ok {
ut.Struct = d.structName
ut.Field = d.fieldName
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
}
}
@ -41,7 +43,7 @@ func (d *unmarshalJSONDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
ptr: newptr,
}))
if err := v.(Unmarshaler).UnmarshalJSON(src); err != nil {
d.annotateError(err)
d.annotateError(s.cursor, err)
return err
}
*(*unsafe.Pointer)(p) = newptr
@ -51,7 +53,7 @@ func (d *unmarshalJSONDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
ptr: p,
}))
if err := v.(Unmarshaler).UnmarshalJSON(src); err != nil {
d.annotateError(err)
d.annotateError(s.cursor, err)
return err
}
}
@ -73,7 +75,7 @@ func (d *unmarshalJSONDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer
ptr: newptr,
}))
if err := v.(Unmarshaler).UnmarshalJSON(src); err != nil {
d.annotateError(err)
d.annotateError(cursor, err)
return 0, err
}
*(*unsafe.Pointer)(p) = newptr
@ -83,7 +85,7 @@ func (d *unmarshalJSONDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer
ptr: p,
}))
if err := v.(Unmarshaler).UnmarshalJSON(src); err != nil {
d.annotateError(err)
d.annotateError(cursor, err)
return 0, err
}
}

View File

@ -22,11 +22,13 @@ func newUnmarshalTextDecoder(typ *rtype, structName, fieldName string) *unmarsha
}
}
func (d *unmarshalTextDecoder) annotateError(err error) {
ut, ok := err.(*UnmarshalTypeError)
if ok {
ut.Struct = d.structName
ut.Field = d.fieldName
func (d *unmarshalTextDecoder) annotateError(cursor int64, err error) {
switch e := err.(type) {
case *UnmarshalTypeError:
e.Struct = d.structName
e.Field = d.fieldName
case *SyntaxError:
e.Offset = cursor
}
}
@ -46,7 +48,7 @@ func (d *unmarshalTextDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
ptr: newptr,
}))
if err := v.(encoding.TextUnmarshaler).UnmarshalText(src); err != nil {
d.annotateError(err)
d.annotateError(s.cursor, err)
return err
}
*(*unsafe.Pointer)(p) = newptr
@ -69,7 +71,7 @@ func (d *unmarshalTextDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer
ptr: *(*unsafe.Pointer)(unsafe.Pointer(&p)),
}))
if err := v.(encoding.TextUnmarshaler).UnmarshalText(src); err != nil {
d.annotateError(err)
d.annotateError(cursor, err)
return 0, err
}
return end, nil

View File

@ -329,7 +329,7 @@ func (n Number) MarshalJSON() ([]byte, error) {
func (n *Number) UnmarshalJSON(b []byte) error {
s := string(b)
if _, err := strconv.ParseFloat(s, 64); err != nil {
return err
return &SyntaxError{msg: err.Error()}
}
*n = Number(s)
return nil