jsoniter_interface_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package jsoniter
  2. import (
  3. "github.com/json-iterator/go/require"
  4. "testing"
  5. "unsafe"
  6. "encoding/json"
  7. )
  8. func Test_write_array_of_interface(t *testing.T) {
  9. should := require.New(t)
  10. array := []interface{}{"hello"}
  11. str, err := MarshalToString(array)
  12. should.Nil(err)
  13. should.Equal(`["hello"]`, str)
  14. }
  15. func Test_write_map_of_interface(t *testing.T) {
  16. should := require.New(t)
  17. val := map[string]interface{}{"hello": "world"}
  18. str, err := MarshalToString(val)
  19. should.Nil(err)
  20. should.Equal(`{"hello":"world"}`, str)
  21. }
  22. func Test_write_map_of_interface_in_struct(t *testing.T) {
  23. type TestObject struct {
  24. Field map[string]interface{}
  25. }
  26. should := require.New(t)
  27. val := TestObject{map[string]interface{}{"hello": "world"}}
  28. str, err := MarshalToString(val)
  29. should.Nil(err)
  30. should.Equal(`{"Field":{"hello":"world"}}`, str)
  31. }
  32. func Test_write_map_of_interface_in_struct_with_two_fields(t *testing.T) {
  33. type TestObject struct {
  34. Field map[string]interface{}
  35. Field2 string
  36. }
  37. should := require.New(t)
  38. val := TestObject{map[string]interface{}{"hello": "world"}, ""}
  39. str, err := MarshalToString(val)
  40. should.Nil(err)
  41. should.Contains(str, `"Field":{"hello":"world"}`)
  42. }
  43. type MyInterface interface {
  44. Hello() string
  45. }
  46. type MyString string
  47. func (ms MyString) Hello() string {
  48. return string(ms)
  49. }
  50. func Test_write_map_of_custom_interface(t *testing.T) {
  51. should := require.New(t)
  52. myStr := MyString("world")
  53. should.Equal("world", myStr.Hello())
  54. val := map[string]MyInterface{"hello": myStr}
  55. str, err := MarshalToString(val)
  56. should.Nil(err)
  57. should.Equal(`{"hello":"world"}`, str)
  58. }
  59. func Test_write_interface(t *testing.T) {
  60. should := require.New(t)
  61. var val interface{}
  62. val = "hello"
  63. str, err := MarshalToString(val)
  64. should.Nil(err)
  65. should.Equal(`"hello"`, str)
  66. }
  67. func Test_read_interface(t *testing.T) {
  68. should := require.New(t)
  69. var val interface{}
  70. err := UnmarshalFromString(`"hello"`, &val)
  71. should.Nil(err)
  72. should.Equal("hello", val)
  73. }
  74. func Test_read_custom_interface(t *testing.T) {
  75. should := require.New(t)
  76. var val MyInterface
  77. RegisterTypeDecoder("jsoniter.MyInterface", func(ptr unsafe.Pointer, iter *Iterator) {
  78. *((*MyInterface)(ptr)) = MyString(iter.ReadString())
  79. })
  80. err := UnmarshalFromString(`"hello"`, &val)
  81. should.Nil(err)
  82. should.Equal("hello", val.Hello())
  83. }
  84. func Test_decode_object_contain_empty_interface(t *testing.T) {
  85. type TestObject struct {
  86. Field interface{}
  87. }
  88. should := require.New(t)
  89. obj := TestObject{}
  90. obj.Field = 1024
  91. should.Nil(UnmarshalFromString(`{"Field": "hello"}`, &obj))
  92. should.Equal("hello", obj.Field)
  93. }
  94. func Test_decode_object_contain_non_empty_interface(t *testing.T) {
  95. type TestObject struct {
  96. Field MyInterface
  97. }
  98. should := require.New(t)
  99. obj := TestObject{}
  100. obj.Field = MyString("abc")
  101. should.Nil(UnmarshalFromString(`{"Field": "hello"}`, &obj))
  102. should.Equal(MyString("hello"), obj.Field)
  103. }
  104. func Test_encode_object_contain_empty_interface(t *testing.T) {
  105. type TestObject struct {
  106. Field interface{}
  107. }
  108. should := require.New(t)
  109. obj := TestObject{}
  110. obj.Field = 1024
  111. str, err := MarshalToString(obj)
  112. should.Nil(err)
  113. should.Equal(`{"Field":1024}`, str)
  114. }
  115. func Test_encode_object_contain_non_empty_interface(t *testing.T) {
  116. type TestObject struct {
  117. Field MyInterface
  118. }
  119. should := require.New(t)
  120. obj := TestObject{}
  121. obj.Field = MyString("hello")
  122. str, err := MarshalToString(obj)
  123. should.Nil(err)
  124. should.Equal(`{"Field":"hello"}`, str)
  125. }
  126. func Test_nil_non_empty_interface(t *testing.T) {
  127. CleanEncoders()
  128. CleanDecoders()
  129. type TestObject struct {
  130. Field []MyInterface
  131. }
  132. should := require.New(t)
  133. obj := TestObject{}
  134. b := []byte(`{"Field":["AAA"]}`)
  135. should.NotNil(json.Unmarshal(b, &obj))
  136. should.NotNil(Unmarshal(b, &obj))
  137. }