jsoniter_reflect_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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_.field2)
  171. }
  172. }
  173. type StructOfTag struct {
  174. field1 string `json:"field-1"`
  175. field2 string `json:"-"`
  176. field3 int `json:",string"`
  177. }
  178. func Test_reflect_struct_tag_field(t *testing.T) {
  179. iter := ParseString(`{"field-1": "hello", "field2": "", "field3": "100"}`)
  180. struct_ := StructOfTag{field2: "world"}
  181. iter.Read(&struct_)
  182. if struct_.field1 != "hello" {
  183. fmt.Println(iter.Error)
  184. t.Fatal(struct_.field1)
  185. }
  186. if struct_.field2 != "world" {
  187. fmt.Println(iter.Error)
  188. t.Fatal(struct_.field2)
  189. }
  190. if struct_.field3 != 100 {
  191. fmt.Println(iter.Error)
  192. t.Fatal(struct_.field3)
  193. }
  194. }
  195. func Test_reflect_slice(t *testing.T) {
  196. iter := ParseString(`["hello", "world"]`)
  197. slice := make([]string, 0, 5)
  198. iter.Read(&slice)
  199. if len(slice) != 2 {
  200. fmt.Println(iter.Error)
  201. t.Fatal(len(slice))
  202. }
  203. if slice[0] != "hello" {
  204. fmt.Println(iter.Error)
  205. t.Fatal(slice[0])
  206. }
  207. if slice[1] != "world" {
  208. fmt.Println(iter.Error)
  209. t.Fatal(slice[1])
  210. }
  211. }
  212. func Test_reflect_large_slice(t *testing.T) {
  213. iter := ParseString(`[1,2,3,4,5,6,7,8,9]`)
  214. slice := make([]int, 0, 1)
  215. iter.Read(&slice)
  216. if len(slice) != 9 {
  217. fmt.Println(iter.Error)
  218. t.Fatal(len(slice))
  219. }
  220. if slice[0] != 1 {
  221. fmt.Println(iter.Error)
  222. t.Fatal(slice[0])
  223. }
  224. if slice[8] != 9 {
  225. fmt.Println(iter.Error)
  226. t.Fatal(slice[1])
  227. }
  228. }
  229. func Test_reflect_nested(t *testing.T) {
  230. iter := ParseString(`[{"field1": "hello"}, null, {"field2": "world"}]`)
  231. slice := []*StructOfString{}
  232. iter.Read(&slice)
  233. if len(slice) != 3 {
  234. fmt.Println(iter.Error)
  235. t.Fatal(len(slice))
  236. }
  237. if slice[0].field1 != "hello" {
  238. fmt.Println(iter.Error)
  239. t.Fatal(slice[0])
  240. }
  241. if slice[1] != nil {
  242. fmt.Println(iter.Error)
  243. t.Fatal(slice[1])
  244. }
  245. if slice[2].field2 != "world" {
  246. fmt.Println(iter.Error)
  247. t.Fatal(slice[1])
  248. }
  249. }
  250. type StructOfTagOne struct {
  251. field3 int `json:"field3,string"`
  252. }
  253. func Benchmark_jsoniter_reflect(b *testing.B) {
  254. b.ReportAllocs()
  255. for n := 0; n < b.N; n++ {
  256. iter := ParseString(`{"field3": "100"}`)
  257. struct_ := StructOfTagOne{}
  258. iter.Read(&struct_)
  259. //iter := ParseString(`[1,2,3]`)
  260. //var array []int
  261. //iter.Read(&array)
  262. }
  263. }
  264. func Benchmark_jsoniter_direct(b *testing.B) {
  265. b.ReportAllocs()
  266. for n := 0; n < b.N; n++ {
  267. //iter := ParseString(`{"field1": "hello", "field2": "world"}`)
  268. //struct_ := StructOfString{}
  269. //for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
  270. // switch field {
  271. // case "field1":
  272. // struct_.field1 = iter.ReadString()
  273. // case "field2":
  274. // struct_.field2 = iter.ReadString()
  275. // default:
  276. // iter.Skip()
  277. // }
  278. //}
  279. iter := ParseString(`["hello", "world"]`)
  280. array := make([]string, 0, 2)
  281. for iter.ReadArray() {
  282. array = append(array, iter.ReadString())
  283. }
  284. }
  285. }
  286. func Benchmark_json_reflect(b *testing.B) {
  287. b.ReportAllocs()
  288. for n := 0; n < b.N; n++ {
  289. struct_ := StructOfTagOne{}
  290. json.Unmarshal([]byte(`{"field3": "100"}`), &struct_)
  291. //array := make([]string, 0, 2)
  292. //json.Unmarshal([]byte(`["hello", "world"]`), &array)
  293. }
  294. }