Prechádzať zdrojové kódy

#159 fix fuzzy decoder, the newIter assigned io.EOF error to original iterator, which stopped further processing

Tao Wen 8 rokov pred
rodič
commit
8c7fc7584a
2 zmenil súbory, kde vykonal 25 pridanie a 3 odobranie
  1. 4 3
      extra/fuzzy_decoder.go
  2. 21 0
      extra/fuzzy_decoder_test.go

+ 4 - 3
extra/fuzzy_decoder.go

@@ -7,6 +7,7 @@ import (
 	"reflect"
 	"strings"
 	"unsafe"
+	"io"
 )
 
 const maxUint = ^uint(0)
@@ -206,7 +207,7 @@ func (decoder *fuzzyIntegerDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.It
 	defer iter.Pool().ReturnIterator(newIter)
 	isFloat := strings.IndexByte(str, '.') != -1
 	decoder.fun(isFloat, ptr, newIter)
-	if newIter.Error != nil {
+	if newIter.Error != nil && newIter.Error != io.EOF {
 		iter.Error = newIter.Error
 	}
 }
@@ -225,7 +226,7 @@ func (decoder *fuzzyFloat32Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.It
 		newIter := iter.Pool().BorrowIterator([]byte(str))
 		defer iter.Pool().ReturnIterator(newIter)
 		*((*float32)(ptr)) = newIter.ReadFloat32()
-		if newIter.Error != nil {
+		if newIter.Error != nil && newIter.Error != io.EOF {
 			iter.Error = newIter.Error
 		}
 	default:
@@ -247,7 +248,7 @@ func (decoder *fuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.It
 		newIter := iter.Pool().BorrowIterator([]byte(str))
 		defer iter.Pool().ReturnIterator(newIter)
 		*((*float64)(ptr)) = newIter.ReadFloat64()
-		if newIter.Error != nil {
+		if newIter.Error != nil && newIter.Error != io.EOF {
 			iter.Error = newIter.Error
 		}
 	default:

+ 21 - 0
extra/fuzzy_decoder_test.go

@@ -257,3 +257,24 @@ func Test_empty_array_as_object(t *testing.T) {
 	should.Nil(jsoniter.UnmarshalFromString(`[]`, &val))
 	should.Equal(struct{}{}, val)
 }
+
+func Test_bad_case(t *testing.T) {
+	var jsonstr = `
+{
+    "extra_type": 181760,
+    "combo_type": 0,
+    "trigger_time_ms": 1498800398000,
+    "_create_time": "2017-06-16 11:21:39",
+    "_msg_type": 41000
+}
+`
+
+	type OrderEventRequestParams struct {
+		ExtraType uint64 `json:"extra_type"`
+	}
+
+	var a OrderEventRequestParams
+	err := jsoniter.UnmarshalFromString(jsonstr, &a)
+	should := require.New(t)
+	should.Nil(err)
+}