Selaa lähdekoodia

#145 interface {} customizatoin is recursive

Tao Wen 8 vuotta sitten
vanhempi
commit
cdbd2ed810
2 muutettua tiedostoa jossa 22 lisäystä ja 23 poistoa
  1. 6 2
      feature_iter.go
  2. 16 21
      jsoniter_customize_test.go

+ 6 - 2
feature_iter.go

@@ -285,14 +285,18 @@ func (iter *Iterator) Read() interface{} {
 	case ArrayValue:
 		arr := []interface{}{}
 		iter.ReadArrayCB(func(iter *Iterator) bool {
-			arr = append(arr, iter.Read())
+			var elem interface{}
+			iter.ReadVal(&elem)
+			arr = append(arr, elem)
 			return true
 		})
 		return arr
 	case ObjectValue:
 		obj := map[string]interface{}{}
 		iter.ReadMapCB(func(Iter *Iterator, field string) bool {
-			obj[field] = iter.Read()
+			var elem interface{}
+			iter.ReadVal(&elem)
+			obj[field] = elem
 			return true
 		})
 		return obj

+ 16 - 21
jsoniter_customize_test.go

@@ -286,27 +286,6 @@ func Test_marshal_json_with_time(t *testing.T) {
 	should.NotNil(obj.TF1.F2)
 }
 
-func Test_unmarshal_empty_interface_as_int64(t *testing.T) {
-	var obj interface{}
-	RegisterTypeDecoderFunc("interface {}", func(ptr unsafe.Pointer, iter *Iterator) {
-		switch iter.WhatIsNext() {
-		case NumberValue:
-			*(*interface{})(ptr) = iter.ReadInt64()
-		default:
-			*(*interface{})(ptr) = iter.Read()
-		}
-	})
-	should := require.New(t)
-	Unmarshal([]byte("100"), &obj)
-	should.Equal(int64(100), obj)
-	Unmarshal([]byte(`"hello"`), &obj)
-	should.Equal("hello", obj)
-	var arr []interface{}
-	Unmarshal([]byte("[100]"), &arr)
-	should.Equal(int64(100), arr[0])
-}
-
-
 func Test_customize_tag_key(t *testing.T) {
 
 	type TestObject struct {
@@ -318,4 +297,20 @@ func Test_customize_tag_key(t *testing.T) {
 	str, err := json.MarshalToString(TestObject{"hello"})
 	should.Nil(err)
 	should.Equal(`{"field":"hello"}`, str)
+}
+
+func Test_recursive_empty_interface_customization(t *testing.T) {
+	t.Skip()
+	var obj interface{}
+	RegisterTypeDecoderFunc("interface {}", func(ptr unsafe.Pointer, iter *Iterator) {
+		switch iter.WhatIsNext() {
+		case NumberValue:
+			*(*interface{})(ptr) = iter.ReadInt64()
+		default:
+			*(*interface{})(ptr) = iter.Read()
+		}
+	})
+	should := require.New(t)
+	Unmarshal([]byte("[100]"), &obj)
+	should.Equal([]interface{}{int64(100)}, obj)
 }