jsoniter_alias_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package jsoniter
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. func Test_alias(t *testing.T) {
  7. should := require.New(t)
  8. type myint int
  9. type myint8 int8
  10. type myint16 int16
  11. type myint32 int32
  12. type myint64 int64
  13. type myuint uint
  14. type myuint8 uint8
  15. type myuint16 uint16
  16. type myuint32 uint32
  17. type myuint64 uint64
  18. type myfloat32 float32
  19. type myfloat64 float64
  20. type mystring string
  21. type mybool bool
  22. type myuintptr uintptr
  23. var a struct {
  24. A myint8 `json:"a"`
  25. B myint16 `json:"stream"`
  26. C myint32 `json:"c"`
  27. D myint64 `json:"d"`
  28. E myuint8 `json:"e"`
  29. F myuint16 `json:"f"`
  30. G myuint32 `json:"g"`
  31. H myuint64 `json:"h"`
  32. I myfloat32 `json:"i"`
  33. J myfloat64 `json:"j"`
  34. K mystring `json:"k"`
  35. L myint `json:"l"`
  36. M myuint `json:"m"`
  37. N mybool `json:"n"`
  38. O myuintptr `json:"o"`
  39. }
  40. should.Nil(UnmarshalFromString(`{"a" : 1, "stream" : 1, "c": 1, "d" : 1, "e" : 1, "f" : 1, "g" : 1, "h": 1, "i" : 1, "j" : 1, "k" :"xxxx", "l" : 1, "m":1, "n": true, "o" : 1}`, &a))
  41. should.Equal(myfloat32(1), a.I)
  42. should.Equal(myfloat64(1), a.J)
  43. should.Equal(myint8(1), a.A)
  44. should.Equal(myint16(1), a.B)
  45. should.Equal(myint32(1), a.C)
  46. should.Equal(myint64(1), a.D)
  47. should.Equal(myuint8(1), a.E)
  48. should.Equal(myuint16(1), a.F)
  49. should.Equal(myuint32(1), a.G)
  50. should.Equal(myuint64(1), a.H)
  51. should.Equal(mystring("xxxx"), a.K)
  52. should.Equal(mybool(true), a.N)
  53. should.Equal(myuintptr(1), a.O)
  54. b, err := Marshal(a)
  55. should.Nil(err)
  56. should.Equal(`{"a":1,"stream":1,"c":1,"d":1,"e":1,"f":1,"g":1,"h":1,"i":1,"j":1,"k":"xxxx","l":1,"m":1,"n":true,"o":1}`, string(b))
  57. }