jsoniter_reflect_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "testing"
  6. "unsafe"
  7. "github.com/json-iterator/go/require"
  8. )
  9. func Test_reflect_one_field_struct(t *testing.T) {
  10. should := require.New(t)
  11. type TestObject struct {
  12. field1 string
  13. }
  14. obj := TestObject{}
  15. should.Nil(UnmarshalString(`{}`, &obj))
  16. should.Equal("", obj.field1)
  17. should.Nil(UnmarshalString(`{"field1": "hello"}`, &obj))
  18. should.Equal("hello", obj.field1)
  19. }
  20. func Test_reflect_two_fields_struct(t *testing.T) {
  21. should := require.New(t)
  22. type TestObject struct {
  23. field1 string
  24. field2 string
  25. }
  26. obj := TestObject{}
  27. should.Nil(UnmarshalString(`{}`, &obj))
  28. should.Equal("", obj.field1)
  29. should.Nil(UnmarshalString(`{"field1": "a", "field2": "b"}`, &obj))
  30. should.Equal("a", obj.field1)
  31. should.Equal("b", obj.field2)
  32. }
  33. func Test_reflect_three_fields_struct(t *testing.T) {
  34. should := require.New(t)
  35. type TestObject struct {
  36. field1 string
  37. field2 string
  38. field3 string
  39. }
  40. obj := TestObject{}
  41. should.Nil(UnmarshalString(`{}`, &obj))
  42. should.Equal("", obj.field1)
  43. should.Nil(UnmarshalString(`{"field1": "a", "field2": "b", "field3": "c"}`, &obj))
  44. should.Equal("a", obj.field1)
  45. should.Equal("b", obj.field2)
  46. should.Equal("c", obj.field3)
  47. }
  48. func Test_reflect_four_fields_struct(t *testing.T) {
  49. should := require.New(t)
  50. type TestObject struct {
  51. field1 string
  52. field2 string
  53. field3 string
  54. field4 string
  55. }
  56. obj := TestObject{}
  57. should.Nil(UnmarshalString(`{}`, &obj))
  58. should.Equal("", obj.field1)
  59. should.Nil(UnmarshalString(`{"field1": "a", "field2": "b", "field3": "c", "field4": "d"}`, &obj))
  60. should.Equal("a", obj.field1)
  61. should.Equal("b", obj.field2)
  62. should.Equal("c", obj.field3)
  63. should.Equal("d", obj.field4)
  64. }
  65. func Test_reflect_five_fields_struct(t *testing.T) {
  66. should := require.New(t)
  67. type TestObject struct {
  68. field1 string
  69. field2 string
  70. field3 string
  71. field4 string
  72. field5 string
  73. }
  74. obj := TestObject{}
  75. should.Nil(UnmarshalString(`{}`, &obj))
  76. should.Equal("", obj.field1)
  77. should.Nil(UnmarshalString(`{"field1": "a", "field2": "b", "field3": "c", "field4": "d", "field5": "e"}`, &obj))
  78. should.Equal("a", obj.field1)
  79. should.Equal("b", obj.field2)
  80. should.Equal("c", obj.field3)
  81. should.Equal("d", obj.field4)
  82. should.Equal("e", obj.field5)
  83. }
  84. func Test_struct_with_optional_field(t *testing.T) {
  85. should := require.New(t)
  86. type TestObject struct {
  87. field1 *string
  88. field2 *string
  89. }
  90. obj := TestObject{}
  91. UnmarshalString(`{"field1": null, "field2": "world"}`, &obj)
  92. should.Nil(obj.field1)
  93. should.Equal("world", *obj.field2)
  94. }
  95. type StructOfTag struct {
  96. Field1 string `json:"field-1"`
  97. Field2 string `json:"-"`
  98. Field3 int `json:",string"`
  99. }
  100. func Test_reflect_struct_tag_field(t *testing.T) {
  101. iter := ParseString(`{"field-1": "hello", "field2": "", "Field3": "100"}`)
  102. Struct := StructOfTag{Field2: "world"}
  103. iter.Read(&Struct)
  104. if Struct.Field1 != "hello" {
  105. fmt.Println(iter.Error)
  106. t.Fatal(Struct.Field1)
  107. }
  108. if Struct.Field2 != "world" {
  109. fmt.Println(iter.Error)
  110. t.Fatal(Struct.Field2)
  111. }
  112. if Struct.Field3 != 100 {
  113. fmt.Println(iter.Error)
  114. t.Fatal(Struct.Field3)
  115. }
  116. }
  117. func Test_reflect_slice(t *testing.T) {
  118. iter := ParseString(`["hello", "world"]`)
  119. slice := make([]string, 0, 5)
  120. iter.Read(&slice)
  121. if len(slice) != 2 {
  122. fmt.Println(iter.Error)
  123. t.Fatal(len(slice))
  124. }
  125. if slice[0] != "hello" {
  126. fmt.Println(iter.Error)
  127. t.Fatal(slice[0])
  128. }
  129. if slice[1] != "world" {
  130. fmt.Println(iter.Error)
  131. t.Fatal(slice[1])
  132. }
  133. }
  134. func Test_reflect_large_slice(t *testing.T) {
  135. iter := ParseString(`[1,2,3,4,5,6,7,8,9]`)
  136. slice := make([]int, 0, 1)
  137. iter.Read(&slice)
  138. if len(slice) != 9 {
  139. fmt.Println(iter.Error)
  140. t.Fatal(len(slice))
  141. }
  142. if slice[0] != 1 {
  143. fmt.Println(iter.Error)
  144. t.Fatal(slice[0])
  145. }
  146. if slice[8] != 9 {
  147. fmt.Println(iter.Error)
  148. t.Fatal(slice[8])
  149. }
  150. }
  151. func Test_reflect_nested(t *testing.T) {
  152. type StructOfString struct {
  153. field1 string
  154. field2 string
  155. }
  156. iter := ParseString(`[{"field1": "hello"}, null, {"field2": "world"}]`)
  157. slice := []*StructOfString{}
  158. iter.Read(&slice)
  159. if len(slice) != 3 {
  160. fmt.Println(iter.Error)
  161. t.Fatal(len(slice))
  162. }
  163. if slice[0].field1 != "hello" {
  164. fmt.Println(iter.Error)
  165. t.Fatal(slice[0])
  166. }
  167. if slice[1] != nil {
  168. fmt.Println(iter.Error)
  169. t.Fatal(slice[1])
  170. }
  171. if slice[2].field2 != "world" {
  172. fmt.Println(iter.Error)
  173. t.Fatal(slice[2])
  174. }
  175. }
  176. func Test_reflect_base64(t *testing.T) {
  177. iter := ParseString(`"YWJj"`)
  178. val := []byte{}
  179. RegisterTypeDecoder("[]uint8", func(ptr unsafe.Pointer, iter *Iterator) {
  180. *((*[]byte)(ptr)) = iter.ReadBase64()
  181. })
  182. defer CleanDecoders()
  183. iter.Read(&val)
  184. if "abc" != string(val) {
  185. t.Fatal(string(val))
  186. }
  187. }
  188. type StructOfTagOne struct {
  189. Field1 string `json:"field1"`
  190. Field2 string `json:"field2"`
  191. Field3 int `json:"field3,string"`
  192. Field4 int `json:"field4,string"`
  193. }
  194. func Benchmark_jsoniter_reflect(b *testing.B) {
  195. b.ReportAllocs()
  196. iter := Create()
  197. Struct := &StructOfTagOne{}
  198. //var Struct *StructOfTagOne
  199. input := []byte(`{"field3": "100", "field4": "100"}`)
  200. //input := []byte(`null`)
  201. for n := 0; n < b.N; n++ {
  202. iter.ResetBytes(input)
  203. iter.Read(&Struct)
  204. }
  205. }
  206. func Benchmark_jsoniter_direct(b *testing.B) {
  207. b.ReportAllocs()
  208. for n := 0; n < b.N; n++ {
  209. //iter := ParseString(`{"field1": "hello", "field2": "world"}`)
  210. //struct_ := StructOfString{}
  211. //for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
  212. // switch field {
  213. // case "field1":
  214. // struct_.Field1 = iter.ReadString()
  215. // case "field2":
  216. // struct_.Field2 = iter.ReadString()
  217. // default:
  218. // iter.Skip()
  219. // }
  220. //}
  221. iter := ParseString(`["hello", "world"]`)
  222. array := make([]string, 0, 2)
  223. for iter.ReadArray() {
  224. array = append(array, iter.ReadString())
  225. }
  226. }
  227. }
  228. func Benchmark_json_reflect(b *testing.B) {
  229. b.ReportAllocs()
  230. for n := 0; n < b.N; n++ {
  231. Struct := StructOfTagOne{}
  232. json.Unmarshal([]byte(`{"field3": "100"}`), &Struct)
  233. //array := make([]string, 0, 2)
  234. //json.Unmarshal([]byte(`["hello", "world"]`), &array)
  235. }
  236. }