1
0
mirror of https://github.com/json-iterator/go.git synced 2025-08-07 21:52:55 +02:00

support TextMarshaler as map key

This commit is contained in:
Tao Wen
2018-02-18 22:49:06 +08:00
parent 577ddede74
commit d8e64aa825
6 changed files with 147 additions and 131 deletions

View File

@ -8,11 +8,23 @@ import (
func init() {
jsonMarshaler := json.Marshaler(fakeJsonMarshaler{})
textMarshaler := encoding.TextMarshaler(fakeTextMarshaler{})
textMarshaler2 := encoding.TextMarshaler(&fakeTextMarshaler2{})
marshalCases = append(marshalCases,
fakeJsonMarshaler{},
&jsonMarshaler,
fakeTextMarshaler{},
&textMarshaler,
fakeTextMarshaler2{},
&textMarshaler2,
map[fakeTextMarshaler]int{
fakeTextMarshaler{}: 100,
},
map[*fakeTextMarshaler]int{
&fakeTextMarshaler{}: 100,
},
map[encoding.TextMarshaler]int{
textMarshaler: 100,
},
)
}
@ -40,3 +52,15 @@ func (q fakeTextMarshaler) MarshalText() ([]byte, error) {
func (q *fakeTextMarshaler) UnmarshalText(value []byte) error {
return nil
}
type fakeTextMarshaler2 struct {
Field2 int
}
func (q *fakeTextMarshaler2) MarshalText() ([]byte, error) {
return []byte(`"abc"`), nil
}
func (q *fakeTextMarshaler2) UnmarshalText(value []byte) error {
return nil
}

View File

@ -54,9 +54,9 @@ func Test_marshal(t *testing.T) {
t.Run(name, func(t *testing.T) {
should := require.New(t)
output1, err1 := json.Marshal(testCase)
should.NoError(err1)
should.NoError(err1, "json")
output2, err2 := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(testCase)
should.NoError(err2)
should.NoError(err2, "jsoniter")
should.Equal(string(output1), string(output2))
})
}