diff --git a/encode_test.go b/encode_test.go index b2cbceb..c7afadb 100644 --- a/encode_test.go +++ b/encode_test.go @@ -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)) +} diff --git a/internal/encoder/compiler.go b/internal/encoder/compiler.go index bf5e0f9..3b3ff3f 100644 --- a/internal/encoder/compiler.go +++ b/internal/encoder/compiler.go @@ -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) }