1
0
mirror of https://github.com/json-iterator/go.git synced 2025-04-20 11:28:49 +02:00

read to interface{}

This commit is contained in:
Tao Wen 2017-01-21 16:09:38 +08:00
parent 928bc4ce72
commit 1d29fa38ef
8 changed files with 71 additions and 40 deletions

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

@ -12,7 +12,7 @@ const (
Invalid ValueType = iota Invalid ValueType = iota
String String
Number Number
Null Nil
Bool Bool
Array Array
Object Object
@ -53,7 +53,7 @@ func init() {
valueTypes['9'] = Number valueTypes['9'] = Number
valueTypes['t'] = Bool valueTypes['t'] = Bool
valueTypes['f'] = Bool valueTypes['f'] = Bool
valueTypes['n'] = Null valueTypes['n'] = Nil
valueTypes['['] = Array valueTypes['['] = Array
valueTypes['{'] = Object valueTypes['{'] = Object
} }
@ -223,7 +223,37 @@ func (iter *Iterator) unreadByte() {
return 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 // ReadBase64 reads a json object as Base64 in byte slice
func (iter *Iterator) ReadBase64() (ret []byte) { func (iter *Iterator) ReadBase64() (ret []byte) {

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

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

@ -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) { func Test_write_float32(t *testing.T) {
vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff, vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001} -0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}

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

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

@ -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) { func Test_write_string(t *testing.T) {
should := require.New(t) should := require.New(t)
buf := &bytes.Buffer{} buf := &bytes.Buffer{}