jsoniter_reflect_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/json-iterator/go/require"
  6. "testing"
  7. "unsafe"
  8. )
  9. func Test_decode_slice(t *testing.T) {
  10. should := require.New(t)
  11. slice := make([]string, 0, 5)
  12. UnmarshalFromString(`["hello", "world"]`, &slice)
  13. should.Equal([]string{"hello", "world"}, slice)
  14. }
  15. func Test_decode_large_slice(t *testing.T) {
  16. should := require.New(t)
  17. slice := make([]int, 0, 1)
  18. UnmarshalFromString(`[1,2,3,4,5,6,7,8,9]`, &slice)
  19. should.Equal([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}, slice)
  20. }
  21. func Test_decode_nested(t *testing.T) {
  22. type StructOfString struct {
  23. field1 string
  24. field2 string
  25. }
  26. iter := ParseString(`[{"field1": "hello"}, null, {"field2": "world"}]`)
  27. slice := []*StructOfString{}
  28. iter.ReadVal(&slice)
  29. if len(slice) != 3 {
  30. fmt.Println(iter.Error)
  31. t.Fatal(len(slice))
  32. }
  33. if slice[0].field1 != "hello" {
  34. fmt.Println(iter.Error)
  35. t.Fatal(slice[0])
  36. }
  37. if slice[1] != nil {
  38. fmt.Println(iter.Error)
  39. t.Fatal(slice[1])
  40. }
  41. if slice[2].field2 != "world" {
  42. fmt.Println(iter.Error)
  43. t.Fatal(slice[2])
  44. }
  45. }
  46. func Test_decode_base64(t *testing.T) {
  47. iter := ParseString(`"YWJj"`)
  48. val := []byte{}
  49. RegisterTypeDecoder("[]uint8", func(ptr unsafe.Pointer, iter *Iterator) {
  50. *((*[]byte)(ptr)) = iter.ReadBase64()
  51. })
  52. defer CleanDecoders()
  53. iter.ReadVal(&val)
  54. if "abc" != string(val) {
  55. t.Fatal(string(val))
  56. }
  57. }
  58. type StructOfTagOne struct {
  59. Field1 string `json:"field1"`
  60. Field2 string `json:"field2"`
  61. Field3 int `json:"field3,string"`
  62. Field4 int `json:"field4,string"`
  63. }
  64. func Benchmark_jsoniter_reflect(b *testing.B) {
  65. b.ReportAllocs()
  66. iter := NewIterator()
  67. Struct := &StructOfTagOne{}
  68. //var Struct *StructOfTagOne
  69. input := []byte(`{"field3": "100", "field4": "100"}`)
  70. //input := []byte(`null`)
  71. for n := 0; n < b.N; n++ {
  72. iter.ResetBytes(input)
  73. iter.ReadVal(&Struct)
  74. }
  75. }
  76. func Benchmark_jsoniter_direct(b *testing.B) {
  77. b.ReportAllocs()
  78. for n := 0; n < b.N; n++ {
  79. //iter := ParseString(`{"field1": "hello", "field2": "world"}`)
  80. //struct_ := StructOfString{}
  81. //for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
  82. // switch field {
  83. // case "field1":
  84. // struct_.Field1 = iter.ReadString()
  85. // case "field2":
  86. // struct_.Field2 = iter.ReadString()
  87. // default:
  88. // iter.Skip()
  89. // }
  90. //}
  91. iter := ParseString(`["hello", "world"]`)
  92. array := make([]string, 0, 2)
  93. for iter.ReadArray() {
  94. array = append(array, iter.ReadString())
  95. }
  96. }
  97. }
  98. func Benchmark_json_reflect(b *testing.B) {
  99. b.ReportAllocs()
  100. for n := 0; n < b.N; n++ {
  101. Struct := StructOfTagOne{}
  102. json.Unmarshal([]byte(`{"field3": "100"}`), &Struct)
  103. //array := make([]string, 0, 2)
  104. //json.Unmarshal([]byte(`["hello", "world"]`), &array)
  105. }
  106. }