jsoniter_reflect_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. func Test_reflect_int8(t *testing.T) {
  33. iter := ParseString(`123`)
  34. val := int8(0)
  35. iter.Read(&val)
  36. if val != 123 {
  37. t.Fatal(val)
  38. }
  39. }
  40. func Test_reflect_int16(t *testing.T) {
  41. iter := ParseString(`123`)
  42. val := int16(0)
  43. iter.Read(&val)
  44. if val != 123 {
  45. t.Fatal(val)
  46. }
  47. }
  48. func Test_reflect_int32(t *testing.T) {
  49. iter := ParseString(`123`)
  50. val := int32(0)
  51. iter.Read(&val)
  52. if val != 123 {
  53. t.Fatal(val)
  54. }
  55. }
  56. func Test_reflect_int64(t *testing.T) {
  57. iter := ParseString(`123`)
  58. val := int64(0)
  59. iter.Read(&val)
  60. if val != 123 {
  61. t.Fatal(val)
  62. }
  63. }
  64. func Test_reflect_uint(t *testing.T) {
  65. iter := ParseString(`123`)
  66. val := uint(0)
  67. iter.Read(&val)
  68. if val != 123 {
  69. t.Fatal(val)
  70. }
  71. }
  72. func Test_reflect_uint8(t *testing.T) {
  73. iter := ParseString(`123`)
  74. val := uint8(0)
  75. iter.Read(&val)
  76. if val != 123 {
  77. t.Fatal(val)
  78. }
  79. }
  80. func Test_reflect_uint16(t *testing.T) {
  81. iter := ParseString(`123`)
  82. val := uint16(0)
  83. iter.Read(&val)
  84. if val != 123 {
  85. t.Fatal(val)
  86. }
  87. }
  88. func Test_reflect_uint32(t *testing.T) {
  89. iter := ParseString(`123`)
  90. val := uint32(0)
  91. iter.Read(&val)
  92. if val != 123 {
  93. t.Fatal(val)
  94. }
  95. }
  96. func Test_reflect_uint64(t *testing.T) {
  97. iter := ParseString(`123`)
  98. val := uint64(0)
  99. iter.Read(&val)
  100. if val != 123 {
  101. t.Fatal(val)
  102. }
  103. }
  104. func Test_reflect_byte(t *testing.T) {
  105. iter := ParseString(`123`)
  106. val := byte(0)
  107. iter.Read(&val)
  108. if val != 123 {
  109. t.Fatal(val)
  110. }
  111. }
  112. func Test_reflect_float32(t *testing.T) {
  113. iter := ParseString(`1.23`)
  114. val := float32(0)
  115. iter.Read(&val)
  116. if val != 1.23 {
  117. fmt.Println(iter.Error)
  118. t.Fatal(val)
  119. }
  120. }
  121. func Test_reflect_float64(t *testing.T) {
  122. iter := ParseString(`1.23`)
  123. val := float64(0)
  124. iter.Read(&val)
  125. if val != 1.23 {
  126. fmt.Println(iter.Error)
  127. t.Fatal(val)
  128. }
  129. }
  130. func Test_reflect_bool(t *testing.T) {
  131. iter := ParseString(`true`)
  132. val := false
  133. iter.Read(&val)
  134. if val != true {
  135. fmt.Println(iter.Error)
  136. t.Fatal(val)
  137. }
  138. }
  139. type StructOfString struct {
  140. field1 string
  141. field2 string
  142. }
  143. func Test_reflect_struct_string(t *testing.T) {
  144. iter := ParseString(`{"field1": "hello", "field2": "world"}`)
  145. struct_ := StructOfString{}
  146. iter.Read(&struct_)
  147. if struct_.field1 != "hello" {
  148. fmt.Println(iter.Error)
  149. t.Fatal(struct_.field1)
  150. }
  151. if struct_.field2 != "world" {
  152. fmt.Println(iter.Error)
  153. t.Fatal(struct_.field1)
  154. }
  155. }
  156. type StructOfStringPtr struct {
  157. field1 *string
  158. field2 *string
  159. }
  160. func Test_reflect_struct_string_ptr(t *testing.T) {
  161. iter := ParseString(`{"field1": null, "field2": "world"}`)
  162. struct_ := StructOfStringPtr{}
  163. iter.Read(&struct_)
  164. if struct_.field1 != nil {
  165. fmt.Println(iter.Error)
  166. t.Fatal(struct_.field1)
  167. }
  168. if *struct_.field2 != "world" {
  169. fmt.Println(iter.Error)
  170. t.Fatal(struct_.field1)
  171. }
  172. }
  173. func Test_reflect_slice(t *testing.T) {
  174. iter := ParseString(`["hello", "world"]`)
  175. slice := make([]string, 0, 1)
  176. iter.Read(&slice)
  177. if len(slice) != 2 {
  178. fmt.Println(iter.Error)
  179. t.Fatal(len(slice))
  180. }
  181. if slice[0] != "hello" {
  182. fmt.Println(iter.Error)
  183. t.Fatal(slice[0])
  184. }
  185. if slice[1] != "world" {
  186. fmt.Println(iter.Error)
  187. t.Fatal(slice[1])
  188. }
  189. }
  190. func Test_reflect_nested(t *testing.T) {
  191. iter := ParseString(`[{"field1": "hello"}, null, {"field2": "world"}]`)
  192. slice := []*StructOfString{}
  193. iter.Read(&slice)
  194. if len(slice) != 3 {
  195. fmt.Println(iter.Error)
  196. t.Fatal(len(slice))
  197. }
  198. if slice[0].field1 != "hello" {
  199. fmt.Println(iter.Error)
  200. t.Fatal(slice[0])
  201. }
  202. if slice[1] != nil {
  203. fmt.Println(iter.Error)
  204. t.Fatal(slice[1])
  205. }
  206. if slice[2].field2 != "world" {
  207. fmt.Println(iter.Error)
  208. t.Fatal(slice[1])
  209. }
  210. }
  211. func Benchmark_jsoniter_reflect(b *testing.B) {
  212. b.ReportAllocs()
  213. for n := 0; n < b.N; n++ {
  214. //iter := ParseString(`{"field1": "hello", "field2": "world"}`)
  215. //struct_ := StructOfString{}
  216. //iter.Read(&struct_)
  217. iter := ParseString(`["hello", "world"]`)
  218. array := make([]string, 0, 1)
  219. iter.Read(&array)
  220. }
  221. }
  222. func Benchmark_jsoniter_direct(b *testing.B) {
  223. b.ReportAllocs()
  224. for n := 0; n < b.N; n++ {
  225. //iter := ParseString(`{"field1": "hello", "field2": "world"}`)
  226. //struct_ := StructOfString{}
  227. //for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
  228. // switch field {
  229. // case "field1":
  230. // struct_.field1 = iter.ReadString()
  231. // case "field2":
  232. // struct_.field2 = iter.ReadString()
  233. // default:
  234. // iter.Skip()
  235. // }
  236. //}
  237. iter := ParseString(`["hello", "world"]`)
  238. array := make([]string, 0, 2)
  239. for iter.ReadArray() {
  240. array = append(array, iter.ReadString())
  241. }
  242. }
  243. }
  244. func Benchmark_json_reflect(b *testing.B) {
  245. b.ReportAllocs()
  246. for n := 0; n < b.N; n++ {
  247. //struct_ := StructOfString{}
  248. //json.Unmarshal([]byte(`{"field1": "hello", "field2": "world"}`), &struct_)
  249. array := make([]string, 0, 2)
  250. json.Unmarshal([]byte(`["hello", "world"]`), &array)
  251. }
  252. }