1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-12 22:47:42 +02:00

Map keys of custom types should serialize using MarshalText when available (#461)

* Map keys of custom types should serialize/deserialize using MarshalText/UnmarshalText when available

- this brings marshaling/unmarshaling behavior in line with encoding/json
- in general, any types that implement the interfaces from the encoding package (TextUnmarshaler, TextMarshaler, etc.) should use the provided method when available
This commit is contained in:
Sai To Yeung
2020-05-07 22:21:59 -04:00
committed by GitHub
parent 0f8241d334
commit 8961be9c21
2 changed files with 55 additions and 36 deletions

View File

@ -31,6 +31,7 @@ func init() {
map[string]*json.RawMessage{"hello": pRawMessage(json.RawMessage("[]"))},
map[Date]bool{{}: true},
map[Date2]bool{{}: true},
map[customKey]string{customKey(1): "bar"},
)
unmarshalCases = append(unmarshalCases, unmarshalCase{
ptr: (*map[string]string)(nil),
@ -55,6 +56,9 @@ func init() {
"2018-12-13": true,
"2018-12-14": true
}`,
}, unmarshalCase{
ptr: (*map[customKey]string)(nil),
input: `{"foo": "bar"}`,
})
}
@ -115,3 +119,14 @@ func (d Date2) UnmarshalJSON(b []byte) error {
func (d Date2) MarshalJSON() ([]byte, error) {
return []byte(d.Time.Format("2006-01-02")), nil
}
type customKey int32
func (c customKey) MarshalText() ([]byte, error) {
return []byte("foo"), nil
}
func (c *customKey) UnmarshalText(value []byte) error {
*c = 1
return nil
}