| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- package jsoniter
- import (
- "testing"
- "fmt"
- "encoding/json"
- )
- func Test_reflect_str(t *testing.T) {
- iter := ParseString(`"hello"`)
- str := ""
- iter.Read(&str)
- if str != "hello" {
- fmt.Println(iter.Error)
- t.Fatal(str)
- }
- }
- func Test_reflect_ptr_str(t *testing.T) {
- iter := ParseString(`"hello"`)
- var str *string
- iter.Read(&str)
- if *str != "hello" {
- t.Fatal(str)
- }
- }
- func Test_reflect_int(t *testing.T) {
- iter := ParseString(`123`)
- val := int(0)
- iter.Read(&val)
- if val != 123 {
- t.Fatal(val)
- }
- }
- type StructOfString struct {
- field1 string
- field2 string
- }
- func Test_reflect_struct_string(t *testing.T) {
- iter := ParseString(`{"field1": "hello", "field2": "world"}`)
- struct_ := StructOfString{}
- 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_.field1)
- }
- }
- 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_slice(t *testing.T) {
- iter := ParseString(`["hello", "world"]`)
- slice := make([]string, 0, 1)
- iter.Read(&slice)
- if len(slice) != 2 {
- fmt.Println(iter.Error)
- t.Fatal(len(slice))
- }
- if slice[0] != "hello" {
- fmt.Println(iter.Error)
- t.Fatal(slice[0])
- }
- if slice[1] != "world" {
- fmt.Println(iter.Error)
- t.Fatal(slice[1])
- }
- }
- func Test_reflect_nested(t *testing.T) {
- iter := ParseString(`[{"field1": "hello"}, null, {"field2": "world"}]`)
- slice := []*StructOfString{}
- iter.Read(&slice)
- if len(slice) != 3 {
- fmt.Println(iter.Error)
- t.Fatal(len(slice))
- }
- if slice[0].field1 != "hello" {
- fmt.Println(iter.Error)
- t.Fatal(slice[0])
- }
- if slice[1] != nil {
- fmt.Println(iter.Error)
- t.Fatal(slice[1])
- }
- if slice[2].field2 != "world" {
- fmt.Println(iter.Error)
- t.Fatal(slice[1])
- }
- }
- 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)
- }
- }
- func Benchmark_jsoniter_direct(b *testing.B) {
- b.ReportAllocs()
- for n := 0; n < b.N; n++ {
- //iter := ParseString(`{"field1": "hello", "field2": "world"}`)
- //struct_ := StructOfString{}
- //for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
- // switch field {
- // case "field1":
- // struct_.field1 = iter.ReadString()
- // case "field2":
- // struct_.field2 = iter.ReadString()
- // default:
- // iter.Skip()
- // }
- //}
- iter := ParseString(`["hello", "world"]`)
- array := make([]string, 0, 2)
- for iter.ReadArray() {
- array = append(array, iter.ReadString())
- }
- }
- }
- 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)
- }
- }
|