Merge pull request #133 from goccy/feature/fix-decoder

Fix decoder (  mainly map type fixing )
This commit is contained in:
Masaaki Goshima 2021-02-15 19:01:33 +09:00 committed by GitHub
commit e080e515fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 105 additions and 83 deletions

View File

@ -150,6 +150,8 @@ func (d *Decoder) Decode(v interface{}) error {
if err := dec.decodeStream(s, header.ptr); err != nil {
return err
}
s.reset()
s.bufSize = initBufSize
return nil
}

View File

@ -1,6 +1,8 @@
package json
import "unsafe"
import (
"unsafe"
)
var (
isWhiteSpace = [256]bool{}
@ -26,69 +28,104 @@ LOOP:
return cursor
}
func skipValue(buf []byte, cursor int64) (int64, error) {
cursor = skipWhiteSpace(buf, cursor)
braceCount := 0
bracketCount := 0
buflen := int64(len(buf))
start := cursor
func skipObject(buf []byte, cursor int64) (int64, error) {
braceCount := 1
for {
switch buf[cursor] {
case nul:
if start == cursor {
return cursor, errUnexpectedEndOfJSON("value of object", cursor)
}
if braceCount == 0 && bracketCount == 0 {
return cursor, nil
}
return cursor, errUnexpectedEndOfJSON("value of object", cursor)
case '{':
braceCount++
case '[':
bracketCount++
case '}':
braceCount--
if braceCount == -1 && bracketCount == 0 {
return cursor, nil
}
case ']':
bracketCount--
if braceCount == 0 && bracketCount == -1 {
return cursor, nil
}
case ',':
if bracketCount == 0 && braceCount == 0 {
return cursor, nil
if braceCount == 0 {
return cursor + 1, nil
}
case '"':
cursor++
for {
cursor++
switch buf[cursor] {
case '"':
if buf[cursor-1] == '\\' {
continue
}
goto SWITCH_OUT
case nul:
return 0, errUnexpectedEndOfJSON("string of object", cursor)
}
}
case nul:
return 0, errUnexpectedEndOfJSON("object of object", cursor)
}
SWITCH_OUT:
cursor++
}
}
for ; cursor < buflen; cursor++ {
if buf[cursor] != '"' {
continue
func skipArray(buf []byte, cursor int64) (int64, error) {
bracketCount := 1
for {
switch buf[cursor] {
case '[':
bracketCount++
case ']':
bracketCount--
if bracketCount == 0 {
return cursor + 1, nil
}
case '"':
for {
cursor++
switch buf[cursor] {
case '"':
if buf[cursor-1] == '\\' {
continue
}
goto SWITCH_OUT
case nul:
return 0, errUnexpectedEndOfJSON("string of object", cursor)
}
if buf[cursor-1] == '\\' {
continue
}
if bracketCount == 0 && braceCount == 0 {
}
case nul:
return 0, errUnexpectedEndOfJSON("array of object", cursor)
}
SWITCH_OUT:
cursor++
}
}
func skipValue(buf []byte, cursor int64) (int64, error) {
for {
switch buf[cursor] {
case ' ', '\t', '\n', '\r':
cursor++
continue
case '{':
return skipObject(buf, cursor+1)
case '[':
return skipArray(buf, cursor+1)
case '"':
for {
cursor++
switch buf[cursor] {
case '"':
if buf[cursor-1] == '\\' {
continue
}
return cursor + 1, nil
case nul:
return 0, errUnexpectedEndOfJSON("string of object", cursor)
}
break
}
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
cursor++
for ; cursor < buflen; cursor++ {
tk := int(buf[cursor])
if (int('0') <= tk && tk <= int('9')) || tk == '.' || tk == 'e' || tk == 'E' {
for {
cursor++
if floatTable[buf[cursor]] {
continue
}
break
}
if bracketCount == 0 && braceCount == 0 {
return cursor, nil
}
continue
return cursor, nil
case 't':
buflen := int64(len(buf))
if cursor+3 >= buflen {
return 0, errUnexpectedEndOfJSON("bool of object", cursor)
}
@ -102,11 +139,9 @@ func skipValue(buf []byte, cursor int64) (int64, error) {
return 0, errUnexpectedEndOfJSON("bool of object", cursor)
}
cursor += 4
if bracketCount == 0 && braceCount == 0 {
return cursor, nil
}
continue
return cursor, nil
case 'f':
buflen := int64(len(buf))
if cursor+4 >= buflen {
return 0, errUnexpectedEndOfJSON("bool of object", cursor)
}
@ -123,11 +158,9 @@ func skipValue(buf []byte, cursor int64) (int64, error) {
return 0, errUnexpectedEndOfJSON("bool of object", cursor)
}
cursor += 5
if bracketCount == 0 && braceCount == 0 {
return cursor, nil
}
continue
return cursor, nil
case 'n':
buflen := int64(len(buf))
if cursor+3 >= buflen {
return 0, errUnexpectedEndOfJSON("null", cursor)
}
@ -141,11 +174,9 @@ func skipValue(buf []byte, cursor int64) (int64, error) {
return 0, errUnexpectedEndOfJSON("null", cursor)
}
cursor += 4
if bracketCount == 0 && braceCount == 0 {
return cursor, nil
}
continue
return cursor, nil
default:
return cursor, errUnexpectedEndOfJSON("null", cursor)
}
cursor++
}
}

View File

@ -33,16 +33,6 @@ func makemap(*rtype, int) unsafe.Pointer
//go:noescape
func mapassign(t *rtype, m unsafe.Pointer, key, val unsafe.Pointer)
func (d *mapDecoder) setKey(buf []byte, cursor int64, key interface{}) (int64, error) {
header := (*interfaceHeader)(unsafe.Pointer(&key))
return d.keyDecoder.decode(buf, cursor, header.ptr)
}
func (d *mapDecoder) setValue(buf []byte, cursor int64, key interface{}) (int64, error) {
header := (*interfaceHeader)(unsafe.Pointer(&key))
return d.valueDecoder.decode(buf, cursor, header.ptr)
}
func (d *mapDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
s.skipWhiteSpace()
switch s.char() {
@ -130,27 +120,23 @@ func (d *mapDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64,
cursor++
return cursor, nil
}
for ; cursor < buflen; cursor++ {
var key interface{}
keyCursor, err := d.setKey(buf, cursor, &key)
for {
k := unsafe_New(d.keyType)
keyCursor, err := d.keyDecoder.decode(buf, cursor, k)
if err != nil {
return 0, err
}
cursor = keyCursor
cursor = skipWhiteSpace(buf, cursor)
cursor = skipWhiteSpace(buf, keyCursor)
if buf[cursor] != ':' {
return 0, errExpected("colon after object key", cursor)
}
cursor++
if cursor >= buflen {
return 0, errUnexpectedEndOfJSON("map", cursor)
}
var value interface{}
valueCursor, err := d.setValue(buf, cursor, &value)
v := unsafe_New(d.valueType)
valueCursor, err := d.valueDecoder.decode(buf, cursor, v)
if err != nil {
return 0, err
}
mapassign(d.mapType, mapValue, unsafe.Pointer(&key), unsafe.Pointer(&value))
mapassign(d.mapType, mapValue, k, v)
cursor = skipWhiteSpace(buf, valueCursor)
if buf[cursor] == '}' {
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue
@ -160,6 +146,6 @@ func (d *mapDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64,
if buf[cursor] != ',' {
return 0, errExpected("comma after object value", cursor)
}
cursor++
}
return cursor, nil
}

View File

@ -483,6 +483,7 @@ func (d *structDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
}
}
s.cursor++
s.skipWhiteSpace()
if s.char() == '}' {
s.cursor++
return nil
@ -551,10 +552,12 @@ func (d *structDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int6
default:
return 0, errNotAtBeginningOfValue(cursor)
}
if buflen < 2 {
return 0, errUnexpectedEndOfJSON("object", cursor)
}
cursor++
cursor = skipWhiteSpace(buf, cursor)
if buf[cursor] == '}' {
cursor++
return cursor, nil
}
for {
c, field, err := d.keyDecoder(d, buf, cursor)
if err != nil {