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

support string conversion

This commit is contained in:
Tao Wen
2016-12-04 22:45:20 +08:00
parent d8153db791
commit d80d954345
2 changed files with 65 additions and 11 deletions

View File

@ -187,9 +187,34 @@ func Test_reflect_struct_string_ptr(t *testing.T) {
t.Fatal(struct_.field1)
}
if *struct_.field2 != "world" {
fmt.Println(iter.Error)
t.Fatal(struct_.field2)
}
}
type StructOfTag struct {
field1 string `json:"field-1"`
field2 string `json:"-"`
field3 int `json:",string"`
}
func Test_reflect_struct_tag_field(t *testing.T) {
iter := ParseString(`{"field-1": "hello", "field2": "", "field3": "100"}`)
struct_ := StructOfTag{field2: "world"}
iter.Read(&struct_)
if struct_.field1 != "hello" {
fmt.Println(iter.Error)
t.Fatal(struct_.field1)
}
if struct_.field2 != "world" {
fmt.Println(iter.Error)
t.Fatal(struct_.field2)
}
if struct_.field3 != 100 {
fmt.Println(iter.Error)
t.Fatal(struct_.field3)
}
}
func Test_reflect_slice(t *testing.T) {
@ -235,12 +260,12 @@ func Test_reflect_nested(t *testing.T) {
func Benchmark_jsoniter_reflect(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
//iter := ParseString(`{"field1": "hello", "field2": "world"}`)
//struct_ := StructOfString{}
//iter.Read(&struct_)
iter := ParseString(`["hello", "world"]`)
array := make([]string, 0, 1)
iter.Read(&array)
iter := ParseString(`{"field3": "100"}`)
struct_ := StructOfTag{}
iter.Read(&struct_)
//iter := ParseString(`["hello", "world"]`)
//array := make([]string, 0, 1)
//iter.Read(&array)
}
}
@ -270,9 +295,9 @@ func Benchmark_jsoniter_direct(b *testing.B) {
func Benchmark_json_reflect(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
//struct_ := StructOfString{}
//json.Unmarshal([]byte(`{"field1": "hello", "field2": "world"}`), &struct_)
array := make([]string, 0, 2)
json.Unmarshal([]byte(`["hello", "world"]`), &array)
struct_ := StructOfTag{}
json.Unmarshal([]byte(`{"field3": "100"}`), &struct_)
//array := make([]string, 0, 2)
//json.Unmarshal([]byte(`["hello", "world"]`), &array)
}
}