jsoniter_reflect_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. func Test_reflect_int(t *testing.T) {
  25. iter := ParseString(`123`)
  26. val := int(0)
  27. iter.Read(&val)
  28. if val != 123 {
  29. t.Fatal(val)
  30. }
  31. }
  32. type StructOfString struct {
  33. field1 string
  34. field2 string
  35. }
  36. func Test_reflect_struct_string(t *testing.T) {
  37. iter := ParseString(`{"field1": "hello", "field2": "world"}`)
  38. struct_ := StructOfString{}
  39. iter.Read(&struct_)
  40. if struct_.field1 != "hello" {
  41. fmt.Println(iter.Error)
  42. t.Fatal(struct_.field1)
  43. }
  44. if struct_.field2 != "world" {
  45. fmt.Println(iter.Error)
  46. t.Fatal(struct_.field1)
  47. }
  48. }
  49. type StructOfStringPtr struct {
  50. field1 *string
  51. field2 *string
  52. }
  53. func Test_reflect_struct_string_ptr(t *testing.T) {
  54. iter := ParseString(`{"field1": null, "field2": "world"}`)
  55. struct_ := StructOfStringPtr{}
  56. iter.Read(&struct_)
  57. if struct_.field1 != nil {
  58. fmt.Println(iter.Error)
  59. t.Fatal(struct_.field1)
  60. }
  61. if *struct_.field2 != "world" {
  62. fmt.Println(iter.Error)
  63. t.Fatal(struct_.field1)
  64. }
  65. }
  66. func Test_reflect_slice(t *testing.T) {
  67. iter := ParseString(`["hello", "world"]`)
  68. slice := make([]string, 0, 1)
  69. iter.Read(&slice)
  70. if len(slice) != 2 {
  71. fmt.Println(iter.Error)
  72. t.Fatal(len(slice))
  73. }
  74. if slice[0] != "hello" {
  75. fmt.Println(iter.Error)
  76. t.Fatal(slice[0])
  77. }
  78. if slice[1] != "world" {
  79. fmt.Println(iter.Error)
  80. t.Fatal(slice[1])
  81. }
  82. }
  83. func Test_reflect_nested(t *testing.T) {
  84. iter := ParseString(`[{"field1": "hello"}, null, {"field2": "world"}]`)
  85. slice := []*StructOfString{}
  86. iter.Read(&slice)
  87. if len(slice) != 3 {
  88. fmt.Println(iter.Error)
  89. t.Fatal(len(slice))
  90. }
  91. if slice[0].field1 != "hello" {
  92. fmt.Println(iter.Error)
  93. t.Fatal(slice[0])
  94. }
  95. if slice[1] != nil {
  96. fmt.Println(iter.Error)
  97. t.Fatal(slice[1])
  98. }
  99. if slice[2].field2 != "world" {
  100. fmt.Println(iter.Error)
  101. t.Fatal(slice[1])
  102. }
  103. }
  104. func Benchmark_jsoniter_reflect(b *testing.B) {
  105. b.ReportAllocs()
  106. for n := 0; n < b.N; n++ {
  107. //iter := ParseString(`{"field1": "hello", "field2": "world"}`)
  108. //struct_ := StructOfString{}
  109. //iter.Read(&struct_)
  110. iter := ParseString(`["hello", "world"]`)
  111. array := make([]string, 0, 1)
  112. iter.Read(&array)
  113. }
  114. }
  115. func Benchmark_jsoniter_direct(b *testing.B) {
  116. b.ReportAllocs()
  117. for n := 0; n < b.N; n++ {
  118. //iter := ParseString(`{"field1": "hello", "field2": "world"}`)
  119. //struct_ := StructOfString{}
  120. //for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
  121. // switch field {
  122. // case "field1":
  123. // struct_.field1 = iter.ReadString()
  124. // case "field2":
  125. // struct_.field2 = iter.ReadString()
  126. // default:
  127. // iter.Skip()
  128. // }
  129. //}
  130. iter := ParseString(`["hello", "world"]`)
  131. array := make([]string, 0, 2)
  132. for iter.ReadArray() {
  133. array = append(array, iter.ReadString())
  134. }
  135. }
  136. }
  137. func Benchmark_json_reflect(b *testing.B) {
  138. b.ReportAllocs()
  139. for n := 0; n < b.N; n++ {
  140. //struct_ := StructOfString{}
  141. //json.Unmarshal([]byte(`{"field1": "hello", "field2": "world"}`), &struct_)
  142. array := make([]string, 0, 2)
  143. json.Unmarshal([]byte(`["hello", "world"]`), &array)
  144. }
  145. }