Skip to content

Commit db32ee8

Browse files
committed
#157 number can be null
1 parent d80309a commit db32ee8

6 files changed

+98
-8
lines changed

feature_iter_skip_strict.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (iter *Iterator) trySkipString() bool {
6464
} else if c == '\\' {
6565
return false
6666
} else if c < ' ' {
67-
iter.ReportError("ReadString",
67+
iter.ReportError("trySkipString",
6868
fmt.Sprintf(`invalid control character found: %d`, c))
6969
return true // already failed
7070
}

feature_iter_string.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (iter *Iterator) readStringSlowPath() (ret string) {
4747
str = append(str, c)
4848
}
4949
}
50-
iter.ReportError("ReadString", "unexpected end of input")
50+
iter.ReportError("readStringSlowPath", "unexpected end of input")
5151
return
5252
}
5353

@@ -104,7 +104,7 @@ func (iter *Iterator) readEscapedChar(c byte, str []byte) []byte {
104104
case 't':
105105
str = append(str, '\t')
106106
default:
107-
iter.ReportError("ReadString",
107+
iter.ReportError("readEscapedChar",
108108
`invalid escape char after \`)
109109
return nil
110110
}
@@ -139,7 +139,7 @@ func (iter *Iterator) ReadStringAsSlice() (ret []byte) {
139139
}
140140
return copied
141141
}
142-
iter.ReportError("ReadString", `expects " or n`)
142+
iter.ReportError("ReadStringAsSlice", `expects " or n`)
143143
return
144144
}
145145

feature_reflect_native.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ type intCodec struct {
3131
}
3232

3333
func (codec *intCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
34+
if iter.ReadNil() {
35+
*((*int)(ptr)) = 0
36+
return
37+
}
3438
*((*int)(ptr)) = iter.ReadInt()
3539
}
3640

@@ -50,6 +54,10 @@ type uintptrCodec struct {
5054
}
5155

5256
func (codec *uintptrCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
57+
if iter.ReadNil() {
58+
*((*uintptr)(ptr)) = 0
59+
return
60+
}
5361
*((*uintptr)(ptr)) = uintptr(iter.ReadUint64())
5462
}
5563

@@ -69,6 +77,10 @@ type int8Codec struct {
6977
}
7078

7179
func (codec *int8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
80+
if iter.ReadNil() {
81+
*((*uint8)(ptr)) = 0
82+
return
83+
}
7284
*((*int8)(ptr)) = iter.ReadInt8()
7385
}
7486

@@ -88,6 +100,10 @@ type int16Codec struct {
88100
}
89101

90102
func (codec *int16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
103+
if iter.ReadNil() {
104+
*((*int16)(ptr)) = 0
105+
return
106+
}
91107
*((*int16)(ptr)) = iter.ReadInt16()
92108
}
93109

@@ -107,6 +123,10 @@ type int32Codec struct {
107123
}
108124

109125
func (codec *int32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
126+
if iter.ReadNil() {
127+
*((*int32)(ptr)) = 0
128+
return
129+
}
110130
*((*int32)(ptr)) = iter.ReadInt32()
111131
}
112132

@@ -126,6 +146,10 @@ type int64Codec struct {
126146
}
127147

128148
func (codec *int64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
149+
if iter.ReadNil() {
150+
*((*int64)(ptr)) = 0
151+
return
152+
}
129153
*((*int64)(ptr)) = iter.ReadInt64()
130154
}
131155

@@ -145,6 +169,10 @@ type uintCodec struct {
145169
}
146170

147171
func (codec *uintCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
172+
if iter.ReadNil() {
173+
*((*uint)(ptr)) = 0
174+
return
175+
}
148176
*((*uint)(ptr)) = iter.ReadUint()
149177
}
150178

@@ -164,6 +192,10 @@ type uint8Codec struct {
164192
}
165193

166194
func (codec *uint8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
195+
if iter.ReadNil() {
196+
*((*uint8)(ptr)) = 0
197+
return
198+
}
167199
*((*uint8)(ptr)) = iter.ReadUint8()
168200
}
169201

@@ -183,6 +215,10 @@ type uint16Codec struct {
183215
}
184216

185217
func (codec *uint16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
218+
if iter.ReadNil() {
219+
*((*uint16)(ptr)) = 0
220+
return
221+
}
186222
*((*uint16)(ptr)) = iter.ReadUint16()
187223
}
188224

@@ -202,6 +238,10 @@ type uint32Codec struct {
202238
}
203239

204240
func (codec *uint32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
241+
if iter.ReadNil() {
242+
*((*uint32)(ptr)) = 0
243+
return
244+
}
205245
*((*uint32)(ptr)) = iter.ReadUint32()
206246
}
207247

@@ -221,6 +261,10 @@ type uint64Codec struct {
221261
}
222262

223263
func (codec *uint64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
264+
if iter.ReadNil() {
265+
*((*uint64)(ptr)) = 0
266+
return
267+
}
224268
*((*uint64)(ptr)) = iter.ReadUint64()
225269
}
226270

@@ -240,6 +284,10 @@ type float32Codec struct {
240284
}
241285

242286
func (codec *float32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
287+
if iter.ReadNil() {
288+
*((*float32)(ptr)) = 0
289+
return
290+
}
243291
*((*float32)(ptr)) = iter.ReadFloat32()
244292
}
245293

@@ -259,6 +307,10 @@ type float64Codec struct {
259307
}
260308

261309
func (codec *float64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
310+
if iter.ReadNil() {
311+
*((*float64)(ptr)) = 0
312+
return
313+
}
262314
*((*float64)(ptr)) = iter.ReadFloat64()
263315
}
264316

jsoniter_customize_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func Test_recursive_empty_interface_customization(t *testing.T) {
316316
}
317317

318318
type GeoLocation struct {
319-
Id string `json:"id,omitempty" db:"id"`
319+
Id string `json:"id,omitempty" db:"id"`
320320
}
321321

322322
func (p *GeoLocation) MarshalJSON() ([]byte, error) {
@@ -337,4 +337,4 @@ func Test_marshal_and_unmarshal_on_non_pointer(t *testing.T) {
337337
err = Unmarshal([]byte("[1]"), &locations)
338338
should.Nil(err)
339339
should.Equal("hello", locations[0].Id)
340-
}
340+
}

jsoniter_int_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,45 @@ func Test_read_uint64_invalid(t *testing.T) {
2020
should.NotNil(iter.Error)
2121
}
2222

23-
func Test_read_int8(t *testing.T) {
23+
func Test_read_int_from_null(t *testing.T) {
24+
25+
type TestObject struct {
26+
F1 int8
27+
F2 int16
28+
F3 int32
29+
F4 int64
30+
F5 int
31+
F6 uint8
32+
F7 uint16
33+
F8 uint32
34+
F9 uint64
35+
F10 uint
36+
F11 float32
37+
F12 float64
38+
F13 uintptr
39+
}
40+
41+
should := require.New(t)
42+
obj := TestObject{}
43+
err := Unmarshal([]byte(`{
44+
"f1":null,
45+
"f2":null,
46+
"f3":null,
47+
"f4":null,
48+
"f5":null,
49+
"f6":null,
50+
"f7":null,
51+
"f8":null,
52+
"f9":null,
53+
"f10":null,
54+
"f11":null,
55+
"f12":null,
56+
"f13":null
57+
}`), &obj)
58+
should.Nil(err)
59+
}
60+
61+
func _int8(t *testing.T) {
2462
inputs := []string{`127`, `-128`}
2563
for _, input := range inputs {
2664
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {

jsoniter_invalid_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package jsoniter
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"github.com/stretchr/testify/require"
67
"io"
78
"testing"
8-
"bytes"
99
)
1010

1111
func Test_missing_object_end(t *testing.T) {

0 commit comments

Comments
 (0)