jsoniter_reflect_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package jsoniter
  2. import (
  3. "testing"
  4. "fmt"
  5. "encoding/json"
  6. )
  7. func Test_reflect_str(t *testing.T) {
  8. iter := ParseString(`"hello"`)
  9. str := ""
  10. iter.Read(&str)
  11. if str != "hello" {
  12. fmt.Println(iter.Error)
  13. t.Fatal(str)
  14. }
  15. }
  16. func Test_reflect_ptr_str(t *testing.T) {
  17. iter := ParseString(`"hello"`)
  18. var str *string
  19. iter.Read(&str)
  20. if *str != "hello" {
  21. t.Fatal(str)
  22. }
  23. }
  24. type StructOfString struct {
  25. field1 string
  26. field2 string
  27. }
  28. func Test_reflect_struct_string(t *testing.T) {
  29. iter := ParseString(`{"field1": "hello", "field2": "world"}`)
  30. struct_ := StructOfString{}
  31. iter.Read(&struct_)
  32. if struct_.field1 != "hello" {
  33. fmt.Println(iter.Error)
  34. t.Fatal(struct_.field1)
  35. }
  36. if struct_.field2 != "world" {
  37. fmt.Println(iter.Error)
  38. t.Fatal(struct_.field1)
  39. }
  40. }
  41. type StructOfStringPtr struct {
  42. field1 *string
  43. field2 *string
  44. }
  45. func Test_reflect_struct_string_ptr(t *testing.T) {
  46. iter := ParseString(`{"field1": null, "field2": "world"}`)
  47. struct_ := StructOfStringPtr{}
  48. iter.Read(&struct_)
  49. if struct_.field1 != nil {
  50. fmt.Println(iter.Error)
  51. t.Fatal(struct_.field1)
  52. }
  53. if *struct_.field2 != "world" {
  54. fmt.Println(iter.Error)
  55. t.Fatal(struct_.field1)
  56. }
  57. }
  58. func Test_reflect_slice(t *testing.T) {
  59. iter := ParseString(`["hello", "world"]`)
  60. array := make([]string, 0, 1)
  61. iter.Read(&array)
  62. if len(array) != 2 {
  63. fmt.Println(iter.Error)
  64. t.Fatal(len(array))
  65. }
  66. if array[0] != "hello" {
  67. fmt.Println(iter.Error)
  68. t.Fatal(array[0])
  69. }
  70. if array[1] != "world" {
  71. fmt.Println(iter.Error)
  72. t.Fatal(array[1])
  73. }
  74. }
  75. func Benchmark_jsoniter_reflect(b *testing.B) {
  76. b.ReportAllocs()
  77. for n := 0; n < b.N; n++ {
  78. //iter := ParseString(`{"field1": "hello", "field2": "world"}`)
  79. //struct_ := StructOfString{}
  80. //iter.Read(&struct_)
  81. iter := ParseString(`["hello", "world"]`)
  82. array := make([]string, 0, 1)
  83. iter.Read(&array)
  84. }
  85. }
  86. func Benchmark_jsoniter_direct(b *testing.B) {
  87. b.ReportAllocs()
  88. for n := 0; n < b.N; n++ {
  89. //iter := ParseString(`{"field1": "hello", "field2": "world"}`)
  90. //struct_ := StructOfString{}
  91. //for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
  92. // switch field {
  93. // case "field1":
  94. // struct_.field1 = iter.ReadString()
  95. // case "field2":
  96. // struct_.field2 = iter.ReadString()
  97. // default:
  98. // iter.Skip()
  99. // }
  100. //}
  101. iter := ParseString(`["hello", "world"]`)
  102. array := make([]string, 0, 2)
  103. for iter.ReadArray() {
  104. array = append(array, iter.ReadString())
  105. }
  106. }
  107. }
  108. func Benchmark_json_reflect(b *testing.B) {
  109. b.ReportAllocs()
  110. for n := 0; n < b.N; n++ {
  111. //struct_ := StructOfString{}
  112. //json.Unmarshal([]byte(`{"field1": "hello", "field2": "world"}`), &struct_)
  113. array := make([]string, 0, 2)
  114. json.Unmarshal([]byte(`["hello", "world"]`), &array)
  115. }
  116. }