mirror of https://github.com/goccy/go-json.git
Merge pull request #136 from goccy/feature/fix-null-string
Fix decoding of type of null string
This commit is contained in:
commit
72cdc2a1d5
|
@ -106,7 +106,7 @@ func decodeCompileMapKey(typ *rtype, structName, fieldName string, structTypeToD
|
|||
case *stringDecoder, *interfaceDecoder:
|
||||
return dec, nil
|
||||
case *boolDecoder, *intDecoder, *uintDecoder, *numberDecoder:
|
||||
return newWrappedStringDecoder(dec, structName, fieldName), nil
|
||||
return newWrappedStringDecoder(typ, dec, structName, fieldName), nil
|
||||
case *ptrDecoder:
|
||||
dec = t.dec
|
||||
default:
|
||||
|
@ -393,7 +393,7 @@ func decodeCompileStruct(typ *rtype, structName, fieldName string, structTypeToD
|
|||
}
|
||||
} else {
|
||||
if tag.isString {
|
||||
dec = newWrappedStringDecoder(dec, structName, field.Name)
|
||||
dec = newWrappedStringDecoder(type2rtype(field.Type), dec, structName, field.Name)
|
||||
}
|
||||
var key string
|
||||
if tag.key != "" {
|
||||
|
|
|
@ -233,7 +233,7 @@ func (d *stringDecoder) decodeStreamByte(s *stream) ([]byte, error) {
|
|||
if err := nullBytes(s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []byte{}, nil
|
||||
return nil, nil
|
||||
case nul:
|
||||
if s.read() {
|
||||
continue
|
||||
|
@ -322,7 +322,7 @@ func (d *stringDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, err
|
|||
return nil, 0, errInvalidCharacter(buf[cursor+3], "null", cursor)
|
||||
}
|
||||
cursor += 4
|
||||
return []byte{}, cursor, nil
|
||||
return nil, cursor, nil
|
||||
default:
|
||||
goto ERROR
|
||||
}
|
||||
|
|
|
@ -2052,7 +2052,6 @@ func TestEmptyString(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// Test that a null for ,string is not replaced with the previous quoted string (issue 7046).
|
||||
// It should also not be an error (issue 2540, issue 8587).
|
||||
func TestNullString(t *testing.T) {
|
||||
|
@ -2074,7 +2073,6 @@ func TestNullString(t *testing.T) {
|
|||
t.Fatalf("after Unmarshal, s.B=%d, s.C=%p, want 1, nil", s.B, s.C)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
func intp(x int) *int {
|
||||
p := new(int)
|
||||
|
|
|
@ -1,22 +1,27 @@
|
|||
package json
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type wrappedStringDecoder struct {
|
||||
typ *rtype
|
||||
dec decoder
|
||||
stringDecoder *stringDecoder
|
||||
structName string
|
||||
fieldName string
|
||||
isPtrType bool
|
||||
}
|
||||
|
||||
func newWrappedStringDecoder(dec decoder, structName, fieldName string) *wrappedStringDecoder {
|
||||
func newWrappedStringDecoder(typ *rtype, dec decoder, structName, fieldName string) *wrappedStringDecoder {
|
||||
return &wrappedStringDecoder{
|
||||
typ: typ,
|
||||
dec: dec,
|
||||
stringDecoder: newStringDecoder(structName, fieldName),
|
||||
structName: structName,
|
||||
fieldName: fieldName,
|
||||
isPtrType: typ.Kind() == reflect.Ptr,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,6 +30,12 @@ func (d *wrappedStringDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if bytes == nil {
|
||||
if d.isPtrType {
|
||||
*(*unsafe.Pointer)(p) = nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
b := make([]byte, len(bytes)+1)
|
||||
copy(b, bytes)
|
||||
if _, err := d.dec.decode(b, 0, p); err != nil {
|
||||
|
@ -38,6 +49,12 @@ func (d *wrappedStringDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer
|
|||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if bytes == nil {
|
||||
if d.isPtrType {
|
||||
*(*unsafe.Pointer)(p) = nil
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
bytes = append(bytes, nul)
|
||||
if _, err := d.dec.decode(bytes, 0, p); err != nil {
|
||||
return 0, err
|
||||
|
|
Loading…
Reference in New Issue