1
0
mirror of https://github.com/json-iterator/go.git synced 2025-05-19 21:53:05 +02:00

#157 number can be null

This commit is contained in:
Tao Wen 2017-09-05 13:00:03 +08:00
parent d80309af3b
commit db32ee8c2d
6 changed files with 98 additions and 8 deletions

View File

@ -64,7 +64,7 @@ func (iter *Iterator) trySkipString() bool {
} else if c == '\\' { } else if c == '\\' {
return false return false
} else if c < ' ' { } else if c < ' ' {
iter.ReportError("ReadString", iter.ReportError("trySkipString",
fmt.Sprintf(`invalid control character found: %d`, c)) fmt.Sprintf(`invalid control character found: %d`, c))
return true // already failed return true // already failed
} }

View File

@ -47,7 +47,7 @@ func (iter *Iterator) readStringSlowPath() (ret string) {
str = append(str, c) str = append(str, c)
} }
} }
iter.ReportError("ReadString", "unexpected end of input") iter.ReportError("readStringSlowPath", "unexpected end of input")
return return
} }
@ -104,7 +104,7 @@ func (iter *Iterator) readEscapedChar(c byte, str []byte) []byte {
case 't': case 't':
str = append(str, '\t') str = append(str, '\t')
default: default:
iter.ReportError("ReadString", iter.ReportError("readEscapedChar",
`invalid escape char after \`) `invalid escape char after \`)
return nil return nil
} }
@ -139,7 +139,7 @@ func (iter *Iterator) ReadStringAsSlice() (ret []byte) {
} }
return copied return copied
} }
iter.ReportError("ReadString", `expects " or n`) iter.ReportError("ReadStringAsSlice", `expects " or n`)
return return
} }

View File

@ -31,6 +31,10 @@ type intCodec struct {
} }
func (codec *intCodec) Decode(ptr unsafe.Pointer, iter *Iterator) { func (codec *intCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if iter.ReadNil() {
*((*int)(ptr)) = 0
return
}
*((*int)(ptr)) = iter.ReadInt() *((*int)(ptr)) = iter.ReadInt()
} }
@ -50,6 +54,10 @@ type uintptrCodec struct {
} }
func (codec *uintptrCodec) Decode(ptr unsafe.Pointer, iter *Iterator) { func (codec *uintptrCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if iter.ReadNil() {
*((*uintptr)(ptr)) = 0
return
}
*((*uintptr)(ptr)) = uintptr(iter.ReadUint64()) *((*uintptr)(ptr)) = uintptr(iter.ReadUint64())
} }
@ -69,6 +77,10 @@ type int8Codec struct {
} }
func (codec *int8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) { func (codec *int8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if iter.ReadNil() {
*((*uint8)(ptr)) = 0
return
}
*((*int8)(ptr)) = iter.ReadInt8() *((*int8)(ptr)) = iter.ReadInt8()
} }
@ -88,6 +100,10 @@ type int16Codec struct {
} }
func (codec *int16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) { func (codec *int16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if iter.ReadNil() {
*((*int16)(ptr)) = 0
return
}
*((*int16)(ptr)) = iter.ReadInt16() *((*int16)(ptr)) = iter.ReadInt16()
} }
@ -107,6 +123,10 @@ type int32Codec struct {
} }
func (codec *int32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) { func (codec *int32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if iter.ReadNil() {
*((*int32)(ptr)) = 0
return
}
*((*int32)(ptr)) = iter.ReadInt32() *((*int32)(ptr)) = iter.ReadInt32()
} }
@ -126,6 +146,10 @@ type int64Codec struct {
} }
func (codec *int64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) { func (codec *int64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if iter.ReadNil() {
*((*int64)(ptr)) = 0
return
}
*((*int64)(ptr)) = iter.ReadInt64() *((*int64)(ptr)) = iter.ReadInt64()
} }
@ -145,6 +169,10 @@ type uintCodec struct {
} }
func (codec *uintCodec) Decode(ptr unsafe.Pointer, iter *Iterator) { func (codec *uintCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if iter.ReadNil() {
*((*uint)(ptr)) = 0
return
}
*((*uint)(ptr)) = iter.ReadUint() *((*uint)(ptr)) = iter.ReadUint()
} }
@ -164,6 +192,10 @@ type uint8Codec struct {
} }
func (codec *uint8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) { func (codec *uint8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if iter.ReadNil() {
*((*uint8)(ptr)) = 0
return
}
*((*uint8)(ptr)) = iter.ReadUint8() *((*uint8)(ptr)) = iter.ReadUint8()
} }
@ -183,6 +215,10 @@ type uint16Codec struct {
} }
func (codec *uint16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) { func (codec *uint16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if iter.ReadNil() {
*((*uint16)(ptr)) = 0
return
}
*((*uint16)(ptr)) = iter.ReadUint16() *((*uint16)(ptr)) = iter.ReadUint16()
} }
@ -202,6 +238,10 @@ type uint32Codec struct {
} }
func (codec *uint32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) { func (codec *uint32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if iter.ReadNil() {
*((*uint32)(ptr)) = 0
return
}
*((*uint32)(ptr)) = iter.ReadUint32() *((*uint32)(ptr)) = iter.ReadUint32()
} }
@ -221,6 +261,10 @@ type uint64Codec struct {
} }
func (codec *uint64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) { func (codec *uint64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if iter.ReadNil() {
*((*uint64)(ptr)) = 0
return
}
*((*uint64)(ptr)) = iter.ReadUint64() *((*uint64)(ptr)) = iter.ReadUint64()
} }
@ -240,6 +284,10 @@ type float32Codec struct {
} }
func (codec *float32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) { func (codec *float32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if iter.ReadNil() {
*((*float32)(ptr)) = 0
return
}
*((*float32)(ptr)) = iter.ReadFloat32() *((*float32)(ptr)) = iter.ReadFloat32()
} }
@ -259,6 +307,10 @@ type float64Codec struct {
} }
func (codec *float64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) { func (codec *float64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if iter.ReadNil() {
*((*float64)(ptr)) = 0
return
}
*((*float64)(ptr)) = iter.ReadFloat64() *((*float64)(ptr)) = iter.ReadFloat64()
} }

View File

@ -316,7 +316,7 @@ func Test_recursive_empty_interface_customization(t *testing.T) {
} }
type GeoLocation struct { type GeoLocation struct {
Id string `json:"id,omitempty" db:"id"` Id string `json:"id,omitempty" db:"id"`
} }
func (p *GeoLocation) MarshalJSON() ([]byte, error) { func (p *GeoLocation) MarshalJSON() ([]byte, error) {

View File

@ -20,7 +20,45 @@ func Test_read_uint64_invalid(t *testing.T) {
should.NotNil(iter.Error) should.NotNil(iter.Error)
} }
func Test_read_int8(t *testing.T) { func Test_read_int_from_null(t *testing.T) {
type TestObject struct {
F1 int8
F2 int16
F3 int32
F4 int64
F5 int
F6 uint8
F7 uint16
F8 uint32
F9 uint64
F10 uint
F11 float32
F12 float64
F13 uintptr
}
should := require.New(t)
obj := TestObject{}
err := Unmarshal([]byte(`{
"f1":null,
"f2":null,
"f3":null,
"f4":null,
"f5":null,
"f6":null,
"f7":null,
"f8":null,
"f9":null,
"f10":null,
"f11":null,
"f12":null,
"f13":null
}`), &obj)
should.Nil(err)
}
func _int8(t *testing.T) {
inputs := []string{`127`, `-128`} inputs := []string{`127`, `-128`}
for _, input := range inputs { for _, input := range inputs {
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) { t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {

View File

@ -1,11 +1,11 @@
package jsoniter package jsoniter
import ( import (
"bytes"
"encoding/json" "encoding/json"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"io" "io"
"testing" "testing"
"bytes"
) )
func Test_missing_object_end(t *testing.T) { func Test_missing_object_end(t *testing.T) {