map_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package test
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/big"
  6. "time"
  7. )
  8. func init() {
  9. var pRawMessage = func(val json.RawMessage) *json.RawMessage {
  10. return &val
  11. }
  12. nilMap := map[string]string(nil)
  13. marshalCases = append(marshalCases,
  14. map[string]interface{}{"abc": 1},
  15. map[string]MyInterface{"hello": MyString("world")},
  16. map[*big.Float]string{big.NewFloat(1.2): "2"},
  17. map[string]interface{}{
  18. "3": 3,
  19. "1": 1,
  20. "2": 2,
  21. },
  22. map[uint64]interface{}{
  23. uint64(1): "a",
  24. uint64(2): "a",
  25. uint64(4): "a",
  26. },
  27. nilMap,
  28. &nilMap,
  29. map[string]*json.RawMessage{"hello": pRawMessage(json.RawMessage("[]"))},
  30. map[Date]bool{{}: true},
  31. map[Date2]bool{{}: true},
  32. map[customKey]string{customKey(1): "bar"},
  33. )
  34. unmarshalCases = append(unmarshalCases, unmarshalCase{
  35. ptr: (*map[string]string)(nil),
  36. input: `{"k\"ey": "val"}`,
  37. }, unmarshalCase{
  38. ptr: (*map[string]string)(nil),
  39. input: `null`,
  40. }, unmarshalCase{
  41. ptr: (*map[string]*json.RawMessage)(nil),
  42. input: "{\"test\":[{\"key\":\"value\"}]}",
  43. }, unmarshalCase{
  44. ptr: (*map[Date]bool)(nil),
  45. input: `{
  46. "2018-12-12": true,
  47. "2018-12-13": true,
  48. "2018-12-14": true
  49. }`,
  50. }, unmarshalCase{
  51. ptr: (*map[Date2]bool)(nil),
  52. input: `{
  53. "2018-12-12": true,
  54. "2018-12-13": true,
  55. "2018-12-14": true
  56. }`,
  57. }, unmarshalCase{
  58. ptr: (*map[customKey]string)(nil),
  59. input: `{"foo": "bar"}`,
  60. })
  61. }
  62. type MyInterface interface {
  63. Hello() string
  64. }
  65. type MyString string
  66. func (ms MyString) Hello() string {
  67. return string(ms)
  68. }
  69. type Date struct {
  70. time.Time
  71. }
  72. func (d *Date) UnmarshalJSON(b []byte) error {
  73. dateStr := string(b) // something like `"2017-08-20"`
  74. if dateStr == "null" {
  75. return nil
  76. }
  77. t, err := time.Parse(`"2006-01-02"`, dateStr)
  78. if err != nil {
  79. return fmt.Errorf("cant parse date: %#v", err)
  80. }
  81. d.Time = t
  82. return nil
  83. }
  84. func (d *Date) MarshalJSON() ([]byte, error) {
  85. return []byte(d.Time.Format("2006-01-02")), nil
  86. }
  87. type Date2 struct {
  88. time.Time
  89. }
  90. func (d Date2) UnmarshalJSON(b []byte) error {
  91. dateStr := string(b) // something like `"2017-08-20"`
  92. if dateStr == "null" {
  93. return nil
  94. }
  95. t, err := time.Parse(`"2006-01-02"`, dateStr)
  96. if err != nil {
  97. return fmt.Errorf("cant parse date: %#v", err)
  98. }
  99. d.Time = t
  100. return nil
  101. }
  102. func (d Date2) MarshalJSON() ([]byte, error) {
  103. return []byte(d.Time.Format("2006-01-02")), nil
  104. }
  105. type customKey int32
  106. func (c customKey) MarshalText() ([]byte, error) {
  107. return []byte("foo"), nil
  108. }
  109. func (c *customKey) UnmarshalText(value []byte) error {
  110. *c = 1
  111. return nil
  112. }