Browse Source

fix #243 fuzzy decoder should take null as valid input

Tao Wen 7 years ago
parent
commit
ad83167dc6
2 changed files with 45 additions and 1 deletions
  1. 13 1
      extra/fuzzy_decoder.go
  2. 32 0
      extra/fuzzy_decoder_test.go

+ 13 - 1
extra/fuzzy_decoder.go

@@ -183,6 +183,9 @@ func (decoder *fuzzyStringDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Ite
 		*((*string)(ptr)) = string(number)
 	case jsoniter.StringValue:
 		*((*string)(ptr)) = iter.ReadString()
+	case jsoniter.NilValue:
+		iter.Skip()
+		*((*string)(ptr)) = ""
 	default:
 		iter.ReportError("fuzzyStringDecoder", "not number or string")
 	}
@@ -208,6 +211,9 @@ func (decoder *fuzzyIntegerDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.It
 		} else {
 			str = "0"
 		}
+	case jsoniter.NilValue:
+		iter.Skip()
+		str = "0"
 	default:
 		iter.ReportError("fuzzyIntegerDecoder", "not number or string")
 	}
@@ -244,6 +250,9 @@ func (decoder *fuzzyFloat32Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.It
 		} else {
 			*((*float32)(ptr)) = 0
 		}
+	case jsoniter.NilValue:
+		iter.Skip()
+		*((*float32)(ptr)) = 0
 	default:
 		iter.ReportError("fuzzyFloat32Decoder", "not number or string")
 	}
@@ -273,7 +282,10 @@ func (decoder *fuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.It
 		} else {
 			*((*float64)(ptr)) = 0
 		}
+	case jsoniter.NilValue:
+		iter.Skip()
+		*((*float64)(ptr)) = 0
 	default:
-		iter.ReportError("fuzzyFloat32Decoder", "not number or string")
+		iter.ReportError("fuzzyFloat64Decoder", "not number or string")
 	}
 }

+ 32 - 0
extra/fuzzy_decoder_test.go

@@ -357,3 +357,35 @@ func Test_bad_case(t *testing.T) {
 	should := require.New(t)
 	should.Nil(err)
 }
+
+func Test_null_to_string(t *testing.T) {
+	should := require.New(t)
+	body := []byte(`null`)
+	var message string
+	err := jsoniter.Unmarshal(body, &message)
+	should.NoError(err)
+}
+
+func Test_null_to_int(t *testing.T) {
+	should := require.New(t)
+	body := []byte(`null`)
+	var message int
+	err := jsoniter.Unmarshal(body, &message)
+	should.NoError(err)
+}
+
+func Test_null_to_float32(t *testing.T) {
+	should := require.New(t)
+	body := []byte(`null`)
+	var message float32
+	err := jsoniter.Unmarshal(body, &message)
+	should.NoError(err)
+}
+
+func Test_null_to_float64(t *testing.T) {
+	should := require.New(t)
+	body := []byte(`null`)
+	var message float64
+	err := jsoniter.Unmarshal(body, &message)
+	should.NoError(err)
+}