fix custom marshal for map key

This commit is contained in:
brongineers 2022-11-13 21:05:50 +03:00
parent 41ad89fe02
commit 705f51716b
2 changed files with 15 additions and 2 deletions

View File

@ -2605,3 +2605,18 @@ func TestIssue386(t *testing.T) {
t.Error(err)
}
}
type customMapKey string
func (b customMapKey) MarshalJSON() ([]byte, error) {
return []byte("[]"), nil
}
func TestCustomMarshalForMapKey(t *testing.T) {
m := map[customMapKey]string{customMapKey("skipcustom"): "test"}
expected, err := stdjson.Marshal(m)
assertErr(t, err)
got, err := json.Marshal(m)
assertErr(t, err)
assertEq(t, "custom map key", string(expected), string(got))
}

View File

@ -506,8 +506,6 @@ func (c *Compiler) listElemCode(typ *runtime.Type) (Code, error) {
func (c *Compiler) mapKeyCode(typ *runtime.Type) (Code, error) {
switch {
case c.implementsMarshalJSON(typ):
return c.marshalJSONCode(typ)
case c.implementsMarshalText(typ):
return c.marshalTextCode(typ)
}