Ver código fonte

read to interface{}

Tao Wen 9 anos atrás
pai
commit
1d29fa38ef

+ 1 - 1
feature_reflect.go

@@ -208,7 +208,7 @@ func (iter *Iterator) ReadAny() (ret *Any) {
 		return MakeAny(iter.ReadString())
 	case Number:
 		return iter.readNumber()
-	case Null:
+	case Nil:
 		return MakeAny(nil)
 	case Bool:
 		return MakeAny(iter.ReadBool())

+ 33 - 3
iterator.go

@@ -12,7 +12,7 @@ const (
 	Invalid ValueType = iota
 	String
 	Number
-	Null
+	Nil
 	Bool
 	Array
 	Object
@@ -53,7 +53,7 @@ func init() {
 	valueTypes['9'] = Number
 	valueTypes['t'] = Bool
 	valueTypes['f'] = Bool
-	valueTypes['n'] = Null
+	valueTypes['n'] = Nil
 	valueTypes['['] = Array
 	valueTypes['{'] = Object
 }
@@ -223,7 +223,37 @@ func (iter *Iterator) unreadByte() {
 	return
 }
 
-
+func (iter *Iterator) Read() interface{} {
+	valueType := iter.WhatIsNext()
+	switch valueType {
+	case String:
+		return iter.ReadString()
+	case Number:
+		return iter.ReadFloat64()
+	case Nil:
+		iter.skipFixedBytes(4) // null
+		return nil
+	case Bool:
+		return iter.ReadBool()
+	case Array:
+		arr := []interface{}{}
+		iter.ReadArrayCB(func(iter *Iterator) bool {
+			arr = append(arr, iter.Read())
+			return true
+		})
+		return arr
+	case Object:
+		obj := map[string]interface{}{}
+		iter.ReadObjectCB(func(Iter *Iterator, field string) bool {
+			obj[field] = iter.Read()
+			return true
+		})
+		return obj
+	default:
+		iter.reportError("Read", fmt.Sprintf("unexpected value type: %v", valueType))
+		return nil
+	}
+}
 
 // ReadBase64 reads a json object as Base64 in byte slice
 func (iter *Iterator) ReadBase64() (ret []byte) {

+ 8 - 18
jsoniter_array_test.go

@@ -33,25 +33,15 @@ func Test_one_element(t *testing.T) {
 }
 
 func Test_two_elements(t *testing.T) {
+	should := require.New(t)
 	iter := ParseString(`[1,2]`)
-	cont := iter.ReadArray()
-	if cont != true {
-		t.FailNow()
-	}
-	if iter.ReadInt64() != 1 {
-		t.FailNow()
-	}
-	cont = iter.ReadArray()
-	if cont != true {
-		t.FailNow()
-	}
-	if iter.ReadInt64() != 2 {
-		t.FailNow()
-	}
-	cont = iter.ReadArray()
-	if cont != false {
-		t.FailNow()
-	}
+	should.True(iter.ReadArray())
+	should.Equal(int64(1), iter.ReadInt64())
+	should.True(iter.ReadArray())
+	should.Equal(int64(2), iter.ReadInt64())
+	should.False(iter.ReadArray())
+	iter = ParseString(`[1,2]`)
+	should.Equal([]interface{}{float64(1), float64(2)}, iter.Read())
 }
 
 func Test_invalid_array(t *testing.T) {

+ 6 - 6
jsoniter_bool_test.go

@@ -7,17 +7,17 @@ import (
 )
 
 func Test_true(t *testing.T) {
+	should := require.New(t)
 	iter := ParseString(`true`)
-	if iter.ReadBool() != true {
-		t.FailNow()
-	}
+	should.True(iter.ReadBool())
+	iter = ParseString(`true`)
+	should.Equal(true, iter.Read())
 }
 
 func Test_false(t *testing.T) {
+	should := require.New(t)
 	iter := ParseString(`false`)
-	if iter.ReadBool() != false {
-		t.FailNow()
-	}
+	should.False(iter.ReadBool())
 }
 
 func Test_write_true_false(t *testing.T) {

+ 6 - 0
jsoniter_float_test.go

@@ -45,6 +45,12 @@ func Test_read_float(t *testing.T) {
 	}
 }
 
+func Test_read_float_via_read(t *testing.T) {
+	should := require.New(t)
+	iter := ParseString(`12.3`)
+	should.Equal(float64(12.3), iter.Read())
+}
+
 func Test_write_float32(t *testing.T) {
 	vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
 	-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}

+ 6 - 8
jsoniter_map_test.go

@@ -8,24 +8,22 @@ import (
 )
 
 func Test_read_map(t *testing.T) {
+	should := require.New(t)
 	iter := ParseString(`{"hello": "world"}`)
 	m := map[string]string{"1": "2"}
 	iter.ReadVal(&m)
 	copy(iter.buf, []byte{0, 0, 0, 0, 0, 0})
-	if !reflect.DeepEqual(map[string]string{"1": "2", "hello": "world"}, m) {
-		fmt.Println(iter.Error)
-		t.Fatal(m)
-	}
+	should.Equal(map[string]string{"1": "2", "hello": "world"}, m)
 }
 
 func Test_read_map_of_interface(t *testing.T) {
+	should := require.New(t)
 	iter := ParseString(`{"hello": "world"}`)
 	m := map[string]interface{}{"1": "2"}
 	iter.ReadVal(&m)
-	if !reflect.DeepEqual(map[string]interface{}{"1": "2", "hello": "world"}, m) {
-		fmt.Println(iter.Error)
-		t.Fatal(m)
-	}
+	should.Equal(map[string]interface{}{"1": "2", "hello": "world"}, m)
+	iter = ParseString(`{"hello": "world"}`)
+	should.Equal(map[string]interface{}{"hello": "world"}, iter.Read())
 }
 
 func Test_read_map_of_any(t *testing.T) {

+ 5 - 4
jsoniter_null_test.go

@@ -6,11 +6,12 @@ import (
 	"bytes"
 )
 
-func Test_decode_null(t *testing.T) {
+func Test_read_null(t *testing.T) {
+	should := require.New(t)
 	iter := ParseString(`null`)
-	if iter.ReadNil() != true {
-		t.FailNow()
-	}
+	should.True(iter.ReadNil())
+	iter = ParseString(`null`)
+	should.Nil(iter.Read())
 }
 
 func Test_write_null(t *testing.T) {

+ 6 - 0
jsoniter_string_test.go

@@ -59,6 +59,12 @@ func Test_read_exotic_string(t *testing.T) {
 	}
 }
 
+func Test_read_string_via_read(t *testing.T) {
+	should := require.New(t)
+	iter := ParseString(`"hello"`)
+	should.Equal("hello", iter.Read())
+}
+
 func Test_write_string(t *testing.T) {
 	should := require.New(t)
 	buf := &bytes.Buffer{}