Skip to content

Commit ad83167

Browse files
committed
fix #243 fuzzy decoder should take null as valid input
1 parent fff342f commit ad83167

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

extra/fuzzy_decoder.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ func (decoder *fuzzyStringDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Ite
183183
*((*string)(ptr)) = string(number)
184184
case jsoniter.StringValue:
185185
*((*string)(ptr)) = iter.ReadString()
186+
case jsoniter.NilValue:
187+
iter.Skip()
188+
*((*string)(ptr)) = ""
186189
default:
187190
iter.ReportError("fuzzyStringDecoder", "not number or string")
188191
}
@@ -208,6 +211,9 @@ func (decoder *fuzzyIntegerDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.It
208211
} else {
209212
str = "0"
210213
}
214+
case jsoniter.NilValue:
215+
iter.Skip()
216+
str = "0"
211217
default:
212218
iter.ReportError("fuzzyIntegerDecoder", "not number or string")
213219
}
@@ -244,6 +250,9 @@ func (decoder *fuzzyFloat32Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.It
244250
} else {
245251
*((*float32)(ptr)) = 0
246252
}
253+
case jsoniter.NilValue:
254+
iter.Skip()
255+
*((*float32)(ptr)) = 0
247256
default:
248257
iter.ReportError("fuzzyFloat32Decoder", "not number or string")
249258
}
@@ -273,7 +282,10 @@ func (decoder *fuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.It
273282
} else {
274283
*((*float64)(ptr)) = 0
275284
}
285+
case jsoniter.NilValue:
286+
iter.Skip()
287+
*((*float64)(ptr)) = 0
276288
default:
277-
iter.ReportError("fuzzyFloat32Decoder", "not number or string")
289+
iter.ReportError("fuzzyFloat64Decoder", "not number or string")
278290
}
279291
}

extra/fuzzy_decoder_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,35 @@ func Test_bad_case(t *testing.T) {
357357
should := require.New(t)
358358
should.Nil(err)
359359
}
360+
361+
func Test_null_to_string(t *testing.T) {
362+
should := require.New(t)
363+
body := []byte(`null`)
364+
var message string
365+
err := jsoniter.Unmarshal(body, &message)
366+
should.NoError(err)
367+
}
368+
369+
func Test_null_to_int(t *testing.T) {
370+
should := require.New(t)
371+
body := []byte(`null`)
372+
var message int
373+
err := jsoniter.Unmarshal(body, &message)
374+
should.NoError(err)
375+
}
376+
377+
func Test_null_to_float32(t *testing.T) {
378+
should := require.New(t)
379+
body := []byte(`null`)
380+
var message float32
381+
err := jsoniter.Unmarshal(body, &message)
382+
should.NoError(err)
383+
}
384+
385+
func Test_null_to_float64(t *testing.T) {
386+
should := require.New(t)
387+
body := []byte(`null`)
388+
var message float64
389+
err := jsoniter.Unmarshal(body, &message)
390+
should.NoError(err)
391+
}

0 commit comments

Comments
 (0)