From 705f51716bfc706f41209b07164a3882b6be2b96 Mon Sep 17 00:00:00 2001 From: brongineers Date: Sun, 13 Nov 2022 21:05:50 +0300 Subject: [PATCH] fix custom marshal for map key --- encode_test.go | 15 +++++++++++++++ internal/encoder/compiler.go | 2 -- 2 files changed, 15 insertions(+), 2 deletions(-) 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) }