jsoniter_customize_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "testing"
  6. "time"
  7. "unsafe"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func Test_customize_type_decoder(t *testing.T) {
  11. RegisterTypeDecoderFunc("time.Time", func(ptr unsafe.Pointer, iter *Iterator) {
  12. t, err := time.ParseInLocation("2006-01-02 15:04:05", iter.ReadString(), time.UTC)
  13. if err != nil {
  14. iter.Error = err
  15. return
  16. }
  17. *((*time.Time)(ptr)) = t
  18. })
  19. defer ConfigDefault.(*frozenConfig).cleanDecoders()
  20. val := time.Time{}
  21. err := Unmarshal([]byte(`"2016-12-05 08:43:28"`), &val)
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. year, month, day := val.Date()
  26. if year != 2016 || month != 12 || day != 5 {
  27. t.Fatal(val)
  28. }
  29. }
  30. func Test_customize_type_encoder(t *testing.T) {
  31. should := require.New(t)
  32. RegisterTypeEncoderFunc("time.Time", func(ptr unsafe.Pointer, stream *Stream) {
  33. t := *((*time.Time)(ptr))
  34. stream.WriteString(t.UTC().Format("2006-01-02 15:04:05"))
  35. }, nil)
  36. defer ConfigDefault.(*frozenConfig).cleanEncoders()
  37. val := time.Unix(0, 0)
  38. str, err := MarshalToString(val)
  39. should.Nil(err)
  40. should.Equal(`"1970-01-01 00:00:00"`, str)
  41. }
  42. func Test_customize_byte_array_encoder(t *testing.T) {
  43. ConfigDefault.(*frozenConfig).cleanEncoders()
  44. should := require.New(t)
  45. RegisterTypeEncoderFunc("[]uint8", func(ptr unsafe.Pointer, stream *Stream) {
  46. t := *((*[]byte)(ptr))
  47. stream.WriteString(string(t))
  48. }, nil)
  49. defer ConfigDefault.(*frozenConfig).cleanEncoders()
  50. val := []byte("abc")
  51. str, err := MarshalToString(val)
  52. should.Nil(err)
  53. should.Equal(`"abc"`, str)
  54. }
  55. func Test_customize_float_marshal(t *testing.T) {
  56. should := require.New(t)
  57. json := Config{MarshalFloatWith6Digits: true}.Froze()
  58. str, err := json.MarshalToString(float32(1.23456789))
  59. should.Nil(err)
  60. should.Equal("1.234568", str)
  61. }
  62. type Tom struct {
  63. field1 string
  64. }
  65. func Test_customize_field_decoder(t *testing.T) {
  66. RegisterFieldDecoderFunc("jsoniter.Tom", "field1", func(ptr unsafe.Pointer, iter *Iterator) {
  67. *((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
  68. })
  69. defer ConfigDefault.(*frozenConfig).cleanDecoders()
  70. tom := Tom{}
  71. err := Unmarshal([]byte(`{"field1": 100}`), &tom)
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. }
  76. type TestObject1 struct {
  77. Field1 string
  78. }
  79. type testExtension struct {
  80. DummyExtension
  81. }
  82. func (extension *testExtension) UpdateStructDescriptor(structDescriptor *StructDescriptor) {
  83. if structDescriptor.Type.String() != "jsoniter.TestObject1" {
  84. return
  85. }
  86. binding := structDescriptor.GetField("Field1")
  87. binding.Encoder = &funcEncoder{fun: func(ptr unsafe.Pointer, stream *Stream) {
  88. str := *((*string)(ptr))
  89. val, _ := strconv.Atoi(str)
  90. stream.WriteInt(val)
  91. }}
  92. binding.Decoder = &funcDecoder{func(ptr unsafe.Pointer, iter *Iterator) {
  93. *((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
  94. }}
  95. binding.ToNames = []string{"field-1"}
  96. binding.FromNames = []string{"field-1"}
  97. }
  98. func Test_customize_field_by_extension(t *testing.T) {
  99. should := require.New(t)
  100. cfg := Config{}.Froze()
  101. cfg.RegisterExtension(&testExtension{})
  102. obj := TestObject1{}
  103. err := cfg.UnmarshalFromString(`{"field-1": 100}`, &obj)
  104. should.Nil(err)
  105. should.Equal("100", obj.Field1)
  106. str, err := cfg.MarshalToString(obj)
  107. should.Nil(err)
  108. should.Equal(`{"field-1":100}`, str)
  109. }
  110. type timeImplementedMarshaler time.Time
  111. func (obj timeImplementedMarshaler) MarshalJSON() ([]byte, error) {
  112. seconds := time.Time(obj).Unix()
  113. return []byte(strconv.FormatInt(seconds, 10)), nil
  114. }
  115. func Test_marshaler(t *testing.T) {
  116. type TestObject struct {
  117. Field timeImplementedMarshaler
  118. }
  119. should := require.New(t)
  120. val := timeImplementedMarshaler(time.Unix(123, 0))
  121. obj := TestObject{val}
  122. bytes, err := json.Marshal(obj)
  123. should.Nil(err)
  124. should.Equal(`{"Field":123}`, string(bytes))
  125. str, err := MarshalToString(obj)
  126. should.Nil(err)
  127. should.Equal(`{"Field":123}`, str)
  128. }
  129. func Test_marshaler_and_encoder(t *testing.T) {
  130. type TestObject struct {
  131. Field *timeImplementedMarshaler
  132. }
  133. ConfigDefault.(*frozenConfig).cleanEncoders()
  134. should := require.New(t)
  135. RegisterTypeEncoderFunc("jsoniter.timeImplementedMarshaler", func(ptr unsafe.Pointer, stream *Stream) {
  136. stream.WriteString("hello from encoder")
  137. }, nil)
  138. val := timeImplementedMarshaler(time.Unix(123, 0))
  139. obj := TestObject{&val}
  140. bytes, err := json.Marshal(obj)
  141. should.Nil(err)
  142. should.Equal(`{"Field":123}`, string(bytes))
  143. str, err := MarshalToString(obj)
  144. should.Nil(err)
  145. should.Equal(`{"Field":"hello from encoder"}`, str)
  146. }
  147. type ObjectImplementedUnmarshaler int
  148. func (obj *ObjectImplementedUnmarshaler) UnmarshalJSON(s []byte) error {
  149. val, _ := strconv.ParseInt(string(s[1:len(s)-1]), 10, 64)
  150. *obj = ObjectImplementedUnmarshaler(val)
  151. return nil
  152. }
  153. func Test_unmarshaler(t *testing.T) {
  154. should := require.New(t)
  155. var obj ObjectImplementedUnmarshaler
  156. err := json.Unmarshal([]byte(` "100" `), &obj)
  157. should.Nil(err)
  158. should.Equal(100, int(obj))
  159. iter := ParseString(ConfigDefault, ` "100" `)
  160. iter.ReadVal(&obj)
  161. should.Nil(err)
  162. should.Equal(100, int(obj))
  163. }
  164. func Test_unmarshaler_and_decoder(t *testing.T) {
  165. type TestObject struct {
  166. Field *ObjectImplementedUnmarshaler
  167. Field2 string
  168. }
  169. ConfigDefault.(*frozenConfig).cleanDecoders()
  170. should := require.New(t)
  171. RegisterTypeDecoderFunc("jsoniter.ObjectImplementedUnmarshaler", func(ptr unsafe.Pointer, iter *Iterator) {
  172. *(*ObjectImplementedUnmarshaler)(ptr) = 10
  173. iter.Skip()
  174. })
  175. obj := TestObject{}
  176. val := ObjectImplementedUnmarshaler(0)
  177. obj.Field = &val
  178. err := json.Unmarshal([]byte(`{"Field":"100"}`), &obj)
  179. should.Nil(err)
  180. should.Equal(100, int(*obj.Field))
  181. err = Unmarshal([]byte(`{"Field":"100"}`), &obj)
  182. should.Nil(err)
  183. should.Equal(10, int(*obj.Field))
  184. }
  185. type tmString string
  186. type tmStruct struct {
  187. String tmString
  188. }
  189. func (s tmStruct) MarshalJSON() ([]byte, error) {
  190. var b []byte
  191. b = append(b, '"')
  192. b = append(b, s.String...)
  193. b = append(b, '"')
  194. return b, nil
  195. }
  196. func Test_marshaler_on_struct(t *testing.T) {
  197. fixed := tmStruct{"hello"}
  198. //json.Marshal(fixed)
  199. Marshal(fixed)
  200. }
  201. type withChan struct {
  202. F2 chan []byte
  203. }
  204. func (q withChan) MarshalJSON() ([]byte, error) {
  205. return []byte(`""`), nil
  206. }
  207. func (q *withChan) UnmarshalJSON(value []byte) error {
  208. return nil
  209. }
  210. func Test_marshal_json_with_chan(t *testing.T) {
  211. type TestObject struct {
  212. F1 withChan
  213. }
  214. should := require.New(t)
  215. output, err := MarshalToString(TestObject{})
  216. should.Nil(err)
  217. should.Equal(`{"F1":""}`, output)
  218. }
  219. type withTime struct {
  220. time.Time
  221. }
  222. func (t *withTime) UnmarshalJSON(b []byte) error {
  223. return nil
  224. }
  225. func (t withTime) MarshalJSON() ([]byte, error) {
  226. return []byte(`"fake"`), nil
  227. }
  228. func Test_marshal_json_with_time(t *testing.T) {
  229. type S1 struct {
  230. F1 withTime
  231. F2 *withTime
  232. }
  233. type TestObject struct {
  234. TF1 S1
  235. }
  236. should := require.New(t)
  237. obj := TestObject{
  238. S1{
  239. F1: withTime{
  240. time.Unix(0, 0),
  241. },
  242. F2: &withTime{
  243. time.Unix(0, 0),
  244. },
  245. },
  246. }
  247. output, err := json.Marshal(obj)
  248. should.Nil(err)
  249. should.Equal(`{"TF1":{"F1":"fake","F2":"fake"}}`, string(output))
  250. output, err = Marshal(obj)
  251. should.Nil(err)
  252. should.Equal(`{"TF1":{"F1":"fake","F2":"fake"}}`, string(output))
  253. obj = TestObject{}
  254. should.Nil(json.Unmarshal([]byte(`{"TF1":{"F1":"fake","F2":"fake"}}`), &obj))
  255. should.NotNil(obj.TF1.F2)
  256. obj = TestObject{}
  257. should.Nil(Unmarshal([]byte(`{"TF1":{"F1":"fake","F2":"fake"}}`), &obj))
  258. should.NotNil(obj.TF1.F2)
  259. }
  260. func Test_customize_tag_key(t *testing.T) {
  261. type TestObject struct {
  262. Field string `orm:"field"`
  263. }
  264. should := require.New(t)
  265. json := Config{TagKey: "orm"}.Froze()
  266. str, err := json.MarshalToString(TestObject{"hello"})
  267. should.Nil(err)
  268. should.Equal(`{"field":"hello"}`, str)
  269. }
  270. func Test_recursive_empty_interface_customization(t *testing.T) {
  271. t.Skip()
  272. var obj interface{}
  273. RegisterTypeDecoderFunc("interface {}", func(ptr unsafe.Pointer, iter *Iterator) {
  274. switch iter.WhatIsNext() {
  275. case NumberValue:
  276. *(*interface{})(ptr) = iter.ReadInt64()
  277. default:
  278. *(*interface{})(ptr) = iter.Read()
  279. }
  280. })
  281. should := require.New(t)
  282. Unmarshal([]byte("[100]"), &obj)
  283. should.Equal([]interface{}{int64(100)}, obj)
  284. }
  285. type GeoLocation struct {
  286. Id string `json:"id,omitempty" db:"id"`
  287. }
  288. func (p *GeoLocation) MarshalJSON() ([]byte, error) {
  289. return []byte(`{}`), nil
  290. }
  291. func (p *GeoLocation) UnmarshalJSON(input []byte) error {
  292. p.Id = "hello"
  293. return nil
  294. }
  295. func Test_marshal_and_unmarshal_on_non_pointer(t *testing.T) {
  296. should := require.New(t)
  297. locations := []GeoLocation{{"000"}}
  298. bytes, err := Marshal(locations)
  299. should.Nil(err)
  300. should.Equal("[{}]", string(bytes))
  301. err = Unmarshal([]byte("[1]"), &locations)
  302. should.Nil(err)
  303. should.Equal("hello", locations[0].Id)
  304. }