Skip to content

Commit

Permalink
fix custom marshal for map key
Browse files Browse the repository at this point in the history
  • Loading branch information
brongineers committed Nov 13, 2022
1 parent 41ad89f commit 705f517
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 15 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
2 changes: 0 additions & 2 deletions internal/encoder/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 705f517

Please sign in to comment.