1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-15 22:50:24 +02:00

support optional string

This commit is contained in:
Tao Wen
2016-12-04 13:13:38 +08:00
parent dd431da523
commit 33e3df45dd
2 changed files with 89 additions and 20 deletions

View File

@ -20,7 +20,7 @@ type StructOfString struct {
field2 string
}
func Test_reflect_struct(t *testing.T) {
func Test_reflect_struct_string(t *testing.T) {
iter := ParseString(`{"field1": "hello", "field2": "world"}`)
struct_ := StructOfString{}
iter.Read(&struct_)
@ -34,6 +34,43 @@ func Test_reflect_struct(t *testing.T) {
}
}
type StructOfStringPtr struct {
field1 *string
field2 *string
}
func Test_reflect_struct_string_ptr(t *testing.T) {
iter := ParseString(`{"field1": null, "field2": "world"}`)
struct_ := StructOfStringPtr{}
iter.Read(&struct_)
if struct_.field1 != nil {
fmt.Println(iter.Error)
t.Fatal(struct_.field1)
}
if *struct_.field2 != "world" {
fmt.Println(iter.Error)
t.Fatal(struct_.field1)
}
}
func Test_reflect_array(t *testing.T) {
iter := ParseString(`{"hello", "world"}`)
array := []string{}
iter.Read(&array)
if len(array) != 2 {
fmt.Println(iter.Error)
t.Fatal(len(array))
}
if array[0] != "hello" {
fmt.Println(iter.Error)
t.Fatal(array[0])
}
if array[1] != "world" {
fmt.Println(iter.Error)
t.Fatal(array[1])
}
}
func Benchmark_jsoniter_reflect(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {