jsoniter_reflect_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package jsoniter
  2. import (
  3. "testing"
  4. "fmt"
  5. "encoding/json"
  6. "unsafe"
  7. )
  8. func Test_reflect_str(t *testing.T) {
  9. iter := ParseString(`"hello"`)
  10. str := ""
  11. iter.Read(&str)
  12. if str != "hello" {
  13. fmt.Println(iter.Error)
  14. t.Fatal(str)
  15. }
  16. }
  17. func Test_reflect_ptr_str(t *testing.T) {
  18. iter := ParseString(`"hello"`)
  19. var str *string
  20. iter.Read(&str)
  21. if *str != "hello" {
  22. t.Fatal(str)
  23. }
  24. }
  25. func Test_reflect_int(t *testing.T) {
  26. iter := ParseString(`123`)
  27. val := int(0)
  28. iter.Read(&val)
  29. if val != 123 {
  30. t.Fatal(val)
  31. }
  32. }
  33. func Test_reflect_int8(t *testing.T) {
  34. iter := ParseString(`123`)
  35. val := int8(0)
  36. iter.Read(&val)
  37. if val != 123 {
  38. t.Fatal(val)
  39. }
  40. }
  41. func Test_reflect_int16(t *testing.T) {
  42. iter := ParseString(`123`)
  43. val := int16(0)
  44. iter.Read(&val)
  45. if val != 123 {
  46. t.Fatal(val)
  47. }
  48. }
  49. func Test_reflect_int32(t *testing.T) {
  50. iter := ParseString(`123`)
  51. val := int32(0)
  52. iter.Read(&val)
  53. if val != 123 {
  54. t.Fatal(val)
  55. }
  56. }
  57. func Test_reflect_int64(t *testing.T) {
  58. iter := ParseString(`123`)
  59. val := int64(0)
  60. iter.Read(&val)
  61. if val != 123 {
  62. t.Fatal(val)
  63. }
  64. }
  65. func Test_reflect_uint(t *testing.T) {
  66. iter := ParseString(`123`)
  67. val := uint(0)
  68. iter.Read(&val)
  69. if val != 123 {
  70. t.Fatal(val)
  71. }
  72. }
  73. func Test_reflect_uint8(t *testing.T) {
  74. iter := ParseString(`123`)
  75. val := uint8(0)
  76. iter.Read(&val)
  77. if val != 123 {
  78. t.Fatal(val)
  79. }
  80. }
  81. func Test_reflect_uint16(t *testing.T) {
  82. iter := ParseString(`123`)
  83. val := uint16(0)
  84. iter.Read(&val)
  85. if val != 123 {
  86. t.Fatal(val)
  87. }
  88. }
  89. func Test_reflect_uint32(t *testing.T) {
  90. iter := ParseString(`123`)
  91. val := uint32(0)
  92. iter.Read(&val)
  93. if val != 123 {
  94. t.Fatal(val)
  95. }
  96. }
  97. func Test_reflect_uint64(t *testing.T) {
  98. iter := ParseString(`123`)
  99. val := uint64(0)
  100. iter.Read(&val)
  101. if val != 123 {
  102. t.Fatal(val)
  103. }
  104. }
  105. func Test_reflect_byte(t *testing.T) {
  106. iter := ParseString(`123`)
  107. val := byte(0)
  108. iter.Read(&val)
  109. if val != 123 {
  110. t.Fatal(val)
  111. }
  112. }
  113. func Test_reflect_float32(t *testing.T) {
  114. iter := ParseString(`1.23`)
  115. val := float32(0)
  116. iter.Read(&val)
  117. if val != 1.23 {
  118. fmt.Println(iter.Error)
  119. t.Fatal(val)
  120. }
  121. }
  122. func Test_reflect_float64(t *testing.T) {
  123. iter := ParseString(`1.23`)
  124. val := float64(0)
  125. iter.Read(&val)
  126. if val != 1.23 {
  127. fmt.Println(iter.Error)
  128. t.Fatal(val)
  129. }
  130. }
  131. func Test_reflect_bool(t *testing.T) {
  132. iter := ParseString(`true`)
  133. val := false
  134. iter.Read(&val)
  135. if val != true {
  136. fmt.Println(iter.Error)
  137. t.Fatal(val)
  138. }
  139. }
  140. type StructOfString struct {
  141. field1 string
  142. field2 string
  143. }
  144. func Test_reflect_struct_string(t *testing.T) {
  145. iter := ParseString(`{"field1": "hello", "field2": "world"}`)
  146. struct_ := StructOfString{}
  147. iter.Read(&struct_)
  148. if struct_.field1 != "hello" {
  149. fmt.Println(iter.Error)
  150. t.Fatal(struct_.field1)
  151. }
  152. if struct_.field2 != "world" {
  153. fmt.Println(iter.Error)
  154. t.Fatal(struct_.field1)
  155. }
  156. }
  157. type StructOfStringPtr struct {
  158. field1 *string
  159. field2 *string
  160. }
  161. func Test_reflect_struct_string_ptr(t *testing.T) {
  162. iter := ParseString(`{"field1": null, "field2": "world"}`)
  163. struct_ := StructOfStringPtr{}
  164. iter.Read(&struct_)
  165. if struct_.field1 != nil {
  166. fmt.Println(iter.Error)
  167. t.Fatal(struct_.field1)
  168. }
  169. if *struct_.field2 != "world" {
  170. fmt.Println(iter.Error)
  171. t.Fatal(struct_.field2)
  172. }
  173. }
  174. type StructOfTag struct {
  175. field1 string `json:"field-1"`
  176. field2 string `json:"-"`
  177. field3 int `json:",string"`
  178. }
  179. func Test_reflect_struct_tag_field(t *testing.T) {
  180. iter := ParseString(`{"field-1": "hello", "field2": "", "field3": "100"}`)
  181. struct_ := StructOfTag{field2: "world"}
  182. iter.Read(&struct_)
  183. if struct_.field1 != "hello" {
  184. fmt.Println(iter.Error)
  185. t.Fatal(struct_.field1)
  186. }
  187. if struct_.field2 != "world" {
  188. fmt.Println(iter.Error)
  189. t.Fatal(struct_.field2)
  190. }
  191. if struct_.field3 != 100 {
  192. fmt.Println(iter.Error)
  193. t.Fatal(struct_.field3)
  194. }
  195. }
  196. func Test_reflect_slice(t *testing.T) {
  197. iter := ParseString(`["hello", "world"]`)
  198. slice := make([]string, 0, 5)
  199. iter.Read(&slice)
  200. if len(slice) != 2 {
  201. fmt.Println(iter.Error)
  202. t.Fatal(len(slice))
  203. }
  204. if slice[0] != "hello" {
  205. fmt.Println(iter.Error)
  206. t.Fatal(slice[0])
  207. }
  208. if slice[1] != "world" {
  209. fmt.Println(iter.Error)
  210. t.Fatal(slice[1])
  211. }
  212. }
  213. func Test_reflect_large_slice(t *testing.T) {
  214. iter := ParseString(`[1,2,3,4,5,6,7,8,9]`)
  215. slice := make([]int, 0, 1)
  216. iter.Read(&slice)
  217. if len(slice) != 9 {
  218. fmt.Println(iter.Error)
  219. t.Fatal(len(slice))
  220. }
  221. if slice[0] != 1 {
  222. fmt.Println(iter.Error)
  223. t.Fatal(slice[0])
  224. }
  225. if slice[8] != 9 {
  226. fmt.Println(iter.Error)
  227. t.Fatal(slice[1])
  228. }
  229. }
  230. func Test_reflect_nested(t *testing.T) {
  231. iter := ParseString(`[{"field1": "hello"}, null, {"field2": "world"}]`)
  232. slice := []*StructOfString{}
  233. iter.Read(&slice)
  234. if len(slice) != 3 {
  235. fmt.Println(iter.Error)
  236. t.Fatal(len(slice))
  237. }
  238. if slice[0].field1 != "hello" {
  239. fmt.Println(iter.Error)
  240. t.Fatal(slice[0])
  241. }
  242. if slice[1] != nil {
  243. fmt.Println(iter.Error)
  244. t.Fatal(slice[1])
  245. }
  246. if slice[2].field2 != "world" {
  247. fmt.Println(iter.Error)
  248. t.Fatal(slice[1])
  249. }
  250. }
  251. func Test_reflect_base64(t *testing.T) {
  252. iter := ParseString(`"YWJj"`)
  253. val := []byte{}
  254. RegisterTypeDecoder("[]uint8", func(ptr unsafe.Pointer, iter *Iterator) {
  255. *((*[]byte)(ptr)) = iter.ReadBase64()
  256. })
  257. defer ClearDecoders()
  258. iter.Read(&val)
  259. if "abc" != string(val) {
  260. t.Fatal(string(val))
  261. }
  262. }
  263. type StructOfTagOne struct {
  264. field1 string `json:"field1"`
  265. field2 string `json:"field2"`
  266. field3 int `json:"field3,string"`
  267. field4 int `json:"field4,string"`
  268. }
  269. func Benchmark_jsoniter_reflect(b *testing.B) {
  270. b.ReportAllocs()
  271. iter := Create()
  272. struct_ := &StructOfTagOne{}
  273. //var struct_ *StructOfTagOne
  274. input := []byte(`{"field3": "100", "field4": "100"}`)
  275. //input := []byte(`null`)
  276. for n := 0; n < b.N; n++ {
  277. iter.ResetBytes(input)
  278. iter.Read(&struct_)
  279. }
  280. }
  281. func Benchmark_jsoniter_direct(b *testing.B) {
  282. b.ReportAllocs()
  283. for n := 0; n < b.N; n++ {
  284. //iter := ParseString(`{"field1": "hello", "field2": "world"}`)
  285. //struct_ := StructOfString{}
  286. //for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
  287. // switch field {
  288. // case "field1":
  289. // struct_.field1 = iter.ReadString()
  290. // case "field2":
  291. // struct_.field2 = iter.ReadString()
  292. // default:
  293. // iter.Skip()
  294. // }
  295. //}
  296. iter := ParseString(`["hello", "world"]`)
  297. array := make([]string, 0, 2)
  298. for iter.ReadArray() {
  299. array = append(array, iter.ReadString())
  300. }
  301. }
  302. }
  303. func Benchmark_json_reflect(b *testing.B) {
  304. b.ReportAllocs()
  305. for n := 0; n < b.N; n++ {
  306. struct_ := StructOfTagOne{}
  307. json.Unmarshal([]byte(`{"field3": "100"}`), &struct_)
  308. //array := make([]string, 0, 2)
  309. //json.Unmarshal([]byte(`["hello", "world"]`), &array)
  310. }
  311. }