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