jsoniter_customize_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "github.com/stretchr/testify/require"
  5. "strconv"
  6. "testing"
  7. "time"
  8. "unsafe"
  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. RegisterExtension(&testExtension{})
  101. obj := TestObject1{}
  102. err := UnmarshalFromString(`{"field-1": 100}`, &obj)
  103. should.Nil(err)
  104. should.Equal("100", obj.field1)
  105. str, err := MarshalToString(obj)
  106. should.Nil(err)
  107. should.Equal(`{"field-1":100}`, str)
  108. }
  109. type timeImplementedMarshaler time.Time
  110. func (obj timeImplementedMarshaler) MarshalJSON() ([]byte, error) {
  111. seconds := time.Time(obj).Unix()
  112. return []byte(strconv.FormatInt(seconds, 10)), nil
  113. }
  114. func Test_marshaler(t *testing.T) {
  115. type TestObject struct {
  116. Field timeImplementedMarshaler
  117. }
  118. should := require.New(t)
  119. val := timeImplementedMarshaler(time.Unix(123, 0))
  120. obj := TestObject{val}
  121. bytes, err := json.Marshal(obj)
  122. should.Nil(err)
  123. should.Equal(`{"Field":123}`, string(bytes))
  124. str, err := MarshalToString(obj)
  125. should.Nil(err)
  126. should.Equal(`{"Field":123}`, str)
  127. }
  128. func Test_marshaler_and_encoder(t *testing.T) {
  129. type TestObject struct {
  130. Field *timeImplementedMarshaler
  131. }
  132. ConfigDefault.(*frozenConfig).cleanEncoders()
  133. should := require.New(t)
  134. RegisterTypeEncoderFunc("jsoniter.timeImplementedMarshaler", func(ptr unsafe.Pointer, stream *Stream) {
  135. stream.WriteString("hello from encoder")
  136. }, nil)
  137. val := timeImplementedMarshaler(time.Unix(123, 0))
  138. obj := TestObject{&val}
  139. bytes, err := json.Marshal(obj)
  140. should.Nil(err)
  141. should.Equal(`{"Field":123}`, string(bytes))
  142. str, err := MarshalToString(obj)
  143. should.Nil(err)
  144. should.Equal(`{"Field":"hello from encoder"}`, str)
  145. }
  146. type ObjectImplementedUnmarshaler int
  147. func (obj *ObjectImplementedUnmarshaler) UnmarshalJSON(s []byte) error {
  148. val, _ := strconv.ParseInt(string(s[1:len(s)-1]), 10, 64)
  149. *obj = ObjectImplementedUnmarshaler(val)
  150. return nil
  151. }
  152. func Test_unmarshaler(t *testing.T) {
  153. should := require.New(t)
  154. var obj ObjectImplementedUnmarshaler
  155. err := json.Unmarshal([]byte(` "100" `), &obj)
  156. should.Nil(err)
  157. should.Equal(100, int(obj))
  158. iter := ParseString(ConfigDefault, ` "100" `)
  159. iter.ReadVal(&obj)
  160. should.Nil(err)
  161. should.Equal(100, int(obj))
  162. }
  163. func Test_unmarshaler_and_decoder(t *testing.T) {
  164. type TestObject struct {
  165. Field *ObjectImplementedUnmarshaler
  166. Field2 string
  167. }
  168. ConfigDefault.(*frozenConfig).cleanDecoders()
  169. should := require.New(t)
  170. RegisterTypeDecoderFunc("jsoniter.ObjectImplementedUnmarshaler", func(ptr unsafe.Pointer, iter *Iterator) {
  171. *(*ObjectImplementedUnmarshaler)(ptr) = 10
  172. iter.Skip()
  173. })
  174. obj := TestObject{}
  175. val := ObjectImplementedUnmarshaler(0)
  176. obj.Field = &val
  177. err := json.Unmarshal([]byte(`{"Field":"100"}`), &obj)
  178. should.Nil(err)
  179. should.Equal(100, int(*obj.Field))
  180. err = Unmarshal([]byte(`{"Field":"100"}`), &obj)
  181. should.Nil(err)
  182. should.Equal(10, int(*obj.Field))
  183. }
  184. type tmString string
  185. type tmStruct struct {
  186. String tmString
  187. }
  188. func (s tmStruct) MarshalJSON() ([]byte, error) {
  189. var b []byte
  190. b = append(b, '"')
  191. b = append(b, s.String...)
  192. b = append(b, '"')
  193. return b, nil
  194. }
  195. func Test_marshaler_on_struct(t *testing.T) {
  196. fixed := tmStruct{"hello"}
  197. //json.Marshal(fixed)
  198. Marshal(fixed)
  199. }
  200. type withChan struct {
  201. F2 chan []byte
  202. }
  203. func (q withChan) MarshalJSON() ([]byte, error) {
  204. return []byte(`""`), nil
  205. }
  206. func (q *withChan) UnmarshalJSON(value []byte) error {
  207. return nil
  208. }
  209. func Test_marshal_json_with_chan(t *testing.T) {
  210. type TestObject struct {
  211. F1 withChan
  212. }
  213. should := require.New(t)
  214. output, err := MarshalToString(TestObject{})
  215. should.Nil(err)
  216. should.Equal(`{"F1":""}`, output)
  217. }
  218. type withTime struct {
  219. time.Time
  220. }
  221. func (t *withTime) UnmarshalJSON(b []byte) error {
  222. return nil
  223. }
  224. func (t withTime) MarshalJSON() ([]byte, error) {
  225. return []byte(`"fake"`), nil
  226. }
  227. func Test_marshal_json_with_time(t *testing.T) {
  228. type S1 struct {
  229. F1 withTime
  230. F2 *withTime
  231. }
  232. type TestObject struct {
  233. TF1 S1
  234. }
  235. should := require.New(t)
  236. obj := TestObject{
  237. S1{
  238. F1: withTime{
  239. time.Unix(0, 0),
  240. },
  241. F2: &withTime{
  242. time.Unix(0, 0),
  243. },
  244. },
  245. }
  246. output, err := json.Marshal(obj)
  247. should.Nil(err)
  248. should.Equal(`{"TF1":{"F1":"fake","F2":"fake"}}`, string(output))
  249. output, err = Marshal(obj)
  250. should.Nil(err)
  251. should.Equal(`{"TF1":{"F1":"fake","F2":"fake"}}`, string(output))
  252. obj = TestObject{}
  253. should.Nil(json.Unmarshal([]byte(`{"TF1":{"F1":"fake","F2":"fake"}}`), &obj))
  254. should.NotNil(obj.TF1.F2)
  255. obj = TestObject{}
  256. should.Nil(Unmarshal([]byte(`{"TF1":{"F1":"fake","F2":"fake"}}`), &obj))
  257. should.NotNil(obj.TF1.F2)
  258. }
  259. func Test_customize_tag_key(t *testing.T) {
  260. type TestObject struct {
  261. Field string `orm:"field"`
  262. }
  263. should := require.New(t)
  264. json := Config{TagKey: "orm"}.Froze()
  265. str, err := json.MarshalToString(TestObject{"hello"})
  266. should.Nil(err)
  267. should.Equal(`{"field":"hello"}`, str)
  268. }
  269. func Test_recursive_empty_interface_customization(t *testing.T) {
  270. t.Skip()
  271. var obj interface{}
  272. RegisterTypeDecoderFunc("interface {}", func(ptr unsafe.Pointer, iter *Iterator) {
  273. switch iter.WhatIsNext() {
  274. case NumberValue:
  275. *(*interface{})(ptr) = iter.ReadInt64()
  276. default:
  277. *(*interface{})(ptr) = iter.Read()
  278. }
  279. })
  280. should := require.New(t)
  281. Unmarshal([]byte("[100]"), &obj)
  282. should.Equal([]interface{}{int64(100)}, obj)
  283. }
  284. type GeoLocation struct {
  285. Id string `json:"id,omitempty" db:"id"`
  286. }
  287. func (p *GeoLocation) MarshalJSON() ([]byte, error) {
  288. return []byte(`{}`), nil
  289. }
  290. func (p *GeoLocation) UnmarshalJSON(input []byte) error {
  291. p.Id = "hello"
  292. return nil
  293. }
  294. func Test_marshal_and_unmarshal_on_non_pointer(t *testing.T) {
  295. should := require.New(t)
  296. locations := []GeoLocation{{"000"}}
  297. bytes, err := Marshal(locations)
  298. should.Nil(err)
  299. should.Equal("[{}]", string(bytes))
  300. err = Unmarshal([]byte("[1]"), &locations)
  301. should.Nil(err)
  302. should.Equal("hello", locations[0].Id)
  303. }