|
@@ -2,7 +2,9 @@ package test
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
|
|
+ "fmt"
|
|
|
"math/big"
|
|
"math/big"
|
|
|
|
|
+ "time"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
func init() {
|
|
@@ -27,6 +29,8 @@ func init() {
|
|
|
nilMap,
|
|
nilMap,
|
|
|
&nilMap,
|
|
&nilMap,
|
|
|
map[string]*json.RawMessage{"hello": pRawMessage(json.RawMessage("[]"))},
|
|
map[string]*json.RawMessage{"hello": pRawMessage(json.RawMessage("[]"))},
|
|
|
|
|
+ map[Date]bool{{}: true},
|
|
|
|
|
+ map[Date2]bool{{}: true},
|
|
|
)
|
|
)
|
|
|
unmarshalCases = append(unmarshalCases, unmarshalCase{
|
|
unmarshalCases = append(unmarshalCases, unmarshalCase{
|
|
|
ptr: (*map[string]string)(nil),
|
|
ptr: (*map[string]string)(nil),
|
|
@@ -37,6 +41,20 @@ func init() {
|
|
|
}, unmarshalCase{
|
|
}, unmarshalCase{
|
|
|
ptr: (*map[string]*json.RawMessage)(nil),
|
|
ptr: (*map[string]*json.RawMessage)(nil),
|
|
|
input: "{\"test\":[{\"key\":\"value\"}]}",
|
|
input: "{\"test\":[{\"key\":\"value\"}]}",
|
|
|
|
|
+ }, unmarshalCase{
|
|
|
|
|
+ ptr: (*map[Date]bool)(nil),
|
|
|
|
|
+ input: `{
|
|
|
|
|
+ "2018-12-12": true,
|
|
|
|
|
+ "2018-12-13": true,
|
|
|
|
|
+ "2018-12-14": true
|
|
|
|
|
+ }`,
|
|
|
|
|
+ }, unmarshalCase{
|
|
|
|
|
+ ptr: (*map[Date2]bool)(nil),
|
|
|
|
|
+ input: `{
|
|
|
|
|
+ "2018-12-12": true,
|
|
|
|
|
+ "2018-12-13": true,
|
|
|
|
|
+ "2018-12-14": true
|
|
|
|
|
+ }`,
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -49,3 +67,51 @@ type MyString string
|
|
|
func (ms MyString) Hello() string {
|
|
func (ms MyString) Hello() string {
|
|
|
return string(ms)
|
|
return string(ms)
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+type Date struct {
|
|
|
|
|
+ time.Time
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (d *Date) UnmarshalJSON(b []byte) error {
|
|
|
|
|
+ dateStr := string(b) // something like `"2017-08-20"`
|
|
|
|
|
+
|
|
|
|
|
+ if dateStr == "null" {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ t, err := time.Parse(`"2006-01-02"`, dateStr)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return fmt.Errorf("cant parse date: %#v", err)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ d.Time = t
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (d *Date) MarshalJSON() ([]byte, error) {
|
|
|
|
|
+ return []byte(d.Time.Format("2006-01-02")), nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+type Date2 struct {
|
|
|
|
|
+ time.Time
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (d Date2) UnmarshalJSON(b []byte) error {
|
|
|
|
|
+ dateStr := string(b) // something like `"2017-08-20"`
|
|
|
|
|
+
|
|
|
|
|
+ if dateStr == "null" {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ t, err := time.Parse(`"2006-01-02"`, dateStr)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return fmt.Errorf("cant parse date: %#v", err)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ d.Time = t
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (d Date2) MarshalJSON() ([]byte, error) {
|
|
|
|
|
+ return []byte(d.Time.Format("2006-01-02")), nil
|
|
|
|
|
+}
|