config_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package test
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/json-iterator/go"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func Test_use_number_for_unmarshal(t *testing.T) {
  9. should := require.New(t)
  10. api := jsoniter.Config{UseNumber: true}.Froze()
  11. var obj interface{}
  12. should.Nil(api.UnmarshalFromString("123", &obj))
  13. should.Equal(json.Number("123"), obj)
  14. }
  15. func Test_customize_float_marshal(t *testing.T) {
  16. should := require.New(t)
  17. json := jsoniter.Config{MarshalFloatWith6Digits: true}.Froze()
  18. str, err := json.MarshalToString(float32(1.23456789))
  19. should.Nil(err)
  20. should.Equal("1.234568", str)
  21. }
  22. func Test_customize_tag_key(t *testing.T) {
  23. type TestObject struct {
  24. Field string `orm:"field"`
  25. }
  26. should := require.New(t)
  27. json := jsoniter.Config{TagKey: "orm"}.Froze()
  28. str, err := json.MarshalToString(TestObject{"hello"})
  29. should.Nil(err)
  30. should.Equal(`{"field":"hello"}`, str)
  31. }
  32. func Test_read_large_number_as_interface(t *testing.T) {
  33. should := require.New(t)
  34. var val interface{}
  35. err := jsoniter.Config{UseNumber: true}.Froze().UnmarshalFromString(`123456789123456789123456789`, &val)
  36. should.Nil(err)
  37. output, err := jsoniter.MarshalToString(val)
  38. should.Nil(err)
  39. should.Equal(`123456789123456789123456789`, output)
  40. }
  41. type caseSensitiveStruct struct {
  42. A string `json:"a"`
  43. B string `json:"b,omitempty"`
  44. C *C `json:"C,omitempty"`
  45. }
  46. type C struct {
  47. D int64 `json:"D,omitempty"`
  48. E *E `json:"e,omitempty"`
  49. }
  50. type E struct {
  51. F string `json:"F,omitempty"`
  52. }
  53. func Test_CaseSensitive(t *testing.T) {
  54. should := require.New(t)
  55. testCases := []struct {
  56. input string
  57. expectedOutput string
  58. caseSensitive bool
  59. }{
  60. {
  61. input: `{"A":"foo","B":"bar"}`,
  62. expectedOutput: `{"a":"foo","b":"bar"}`,
  63. caseSensitive: false,
  64. },
  65. {
  66. input: `{"a":"foo","b":"bar"}`,
  67. expectedOutput: `{"a":"foo","b":"bar"}`,
  68. caseSensitive: true,
  69. },
  70. {
  71. input: `{"a":"foo","b":"bar","C":{"D":10}}`,
  72. expectedOutput: `{"a":"foo","b":"bar","C":{"D":10}}`,
  73. caseSensitive: true,
  74. },
  75. {
  76. input: `{"a":"foo","B":"bar","c":{"d":10}}`,
  77. expectedOutput: `{"a":"foo"}`,
  78. caseSensitive: true,
  79. },
  80. {
  81. input: `{"a":"foo","C":{"d":10}}`,
  82. expectedOutput: `{"a":"foo","C":{}}`,
  83. caseSensitive: true,
  84. },
  85. {
  86. input: `{"a":"foo","C":{"D":10,"e":{"f":"baz"}}}`,
  87. expectedOutput: `{"a":"foo","C":{"D":10,"e":{}}}`,
  88. caseSensitive: true,
  89. },
  90. {
  91. input: `{"a":"foo","C":{"D":10,"e":{"F":"baz"}}}`,
  92. expectedOutput: `{"a":"foo","C":{"D":10,"e":{"F":"baz"}}}`,
  93. caseSensitive: true,
  94. },
  95. {
  96. input: `{"A":"foo","c":{"d":10,"E":{"f":"baz"}}}`,
  97. expectedOutput: `{"a":"foo","C":{"D":10,"e":{"F":"baz"}}}`,
  98. caseSensitive: false,
  99. },
  100. }
  101. for _, tc := range testCases {
  102. val := caseSensitiveStruct{}
  103. err := jsoniter.Config{CaseSensitive: tc.caseSensitive}.Froze().UnmarshalFromString(tc.input, &val)
  104. should.Nil(err)
  105. output, err := jsoniter.MarshalToString(val)
  106. should.Nil(err)
  107. should.Equal(tc.expectedOutput, output)
  108. }
  109. }
  110. type structWithElevenFields struct {
  111. A string `json:"A,omitempty"`
  112. B string `json:"B,omitempty"`
  113. C string `json:"C,omitempty"`
  114. D string `json:"d,omitempty"`
  115. E string `json:"e,omitempty"`
  116. F string `json:"f,omitempty"`
  117. G string `json:"g,omitempty"`
  118. H string `json:"h,omitempty"`
  119. I string `json:"i,omitempty"`
  120. J string `json:"j,omitempty"`
  121. K string `json:"k,omitempty"`
  122. }
  123. func Test_CaseSensitive_MoreThanTenFields(t *testing.T) {
  124. should := require.New(t)
  125. testCases := []struct {
  126. input string
  127. expectedOutput string
  128. caseSensitive bool
  129. }{
  130. {
  131. input: `{"A":"1","B":"2","C":"3","d":"4","e":"5","f":"6","g":"7","h":"8","i":"9","j":"10","k":"11"}`,
  132. expectedOutput: `{"A":"1","B":"2","C":"3","d":"4","e":"5","f":"6","g":"7","h":"8","i":"9","j":"10","k":"11"}`,
  133. caseSensitive: true,
  134. },
  135. {
  136. input: `{"a":"1","b":"2","c":"3","D":"4","E":"5","F":"6"}`,
  137. expectedOutput: `{"A":"1","B":"2","C":"3","d":"4","e":"5","f":"6"}`,
  138. caseSensitive: false,
  139. },
  140. {
  141. input: `{"A":"1","b":"2","d":"4","E":"5"}`,
  142. expectedOutput: `{"A":"1","d":"4"}`,
  143. caseSensitive: true,
  144. },
  145. }
  146. for _, tc := range testCases {
  147. val := structWithElevenFields{}
  148. err := jsoniter.Config{CaseSensitive: tc.caseSensitive}.Froze().UnmarshalFromString(tc.input, &val)
  149. should.Nil(err)
  150. output, err := jsoniter.MarshalToString(val)
  151. should.Nil(err)
  152. should.Equal(tc.expectedOutput, output)
  153. }
  154. }
  155. type onlyTaggedFieldStruct struct {
  156. A string `json:"a"`
  157. B string
  158. FSimpl F `json:"f_simpl"`
  159. ISimpl I
  160. FPtr *F `json:"f_ptr"`
  161. IPtr *I
  162. F
  163. *I
  164. }
  165. type F struct {
  166. G string `json:"g"`
  167. H string
  168. }
  169. type I struct {
  170. J string `json:"j"`
  171. K string
  172. }
  173. func Test_OnlyTaggedField(t *testing.T) {
  174. should := require.New(t)
  175. obj := onlyTaggedFieldStruct{
  176. A: "a",
  177. B: "b",
  178. FSimpl: F{G: "g", H: "h"},
  179. ISimpl: I{J: "j", K: "k"},
  180. FPtr: &F{G: "g", H: "h"},
  181. IPtr: &I{J: "j", K: "k"},
  182. F: F{G: "g", H: "h"},
  183. I: &I{J: "j", K: "k"},
  184. }
  185. output, err := jsoniter.Config{OnlyTaggedField: true}.Froze().Marshal(obj)
  186. should.Nil(err)
  187. m := make(map[string]interface{})
  188. err = jsoniter.Unmarshal(output, &m)
  189. should.Nil(err)
  190. should.Equal(map[string]interface{}{
  191. "a": "a",
  192. "f_simpl": map[string]interface{}{
  193. "g": "g",
  194. },
  195. "f_ptr": map[string]interface{}{
  196. "g": "g",
  197. },
  198. "g": "g",
  199. "j": "j",
  200. }, m)
  201. }