1
0
mirror of https://github.com/json-iterator/go.git synced 2025-02-01 19:14:29 +02:00
json-iterator/jsoniter_any_test.go

43 lines
780 B
Go
Raw Normal View History

2016-12-10 23:59:40 +08:00
package jsoniter
2016-12-11 15:53:35 +08:00
import (
"fmt"
"testing"
2016-12-11 15:53:35 +08:00
)
2016-12-10 23:59:40 +08:00
func Test_read_string_as_any(t *testing.T) {
iter := ParseString(`[1, {"hello": "world"}, 2]`)
any := iter.ReadAny()
if iter.Error != nil {
t.Fatal(iter.Error)
}
2016-12-11 10:04:26 +08:00
if any.ToString(1, "hello") != "world" {
2016-12-10 23:59:40 +08:00
t.FailNow()
}
}
func Test_read_float64_as_any(t *testing.T) {
iter := ParseString(`1.23`)
any := iter.ReadAny()
2016-12-11 10:04:26 +08:00
if any.ToFloat32() != 1.23 {
2016-12-10 23:59:40 +08:00
t.FailNow()
}
}
func Test_read_int_as_any(t *testing.T) {
iter := ParseString(`123`)
any := iter.ReadAny()
2016-12-11 10:04:26 +08:00
if any.ToFloat32() != 123 {
2016-12-10 23:59:40 +08:00
t.FailNow()
}
}
2016-12-11 15:53:35 +08:00
func Test_read_any_from_nested(t *testing.T) {
iter := ParseString(`{"numbers": ["1", "2", ["3", "4"]]}`)
val := iter.ReadAny()
if val.ToInt("numbers", 2, 0) != 3 {
fmt.Println(val.Error)
t.FailNow()
}
}