Skip to content

Commit 05d041d

Browse files
committed
fix #313 support json marshaller type as map key
1 parent 5916df6 commit 05d041d

File tree

6 files changed

+102
-24
lines changed

6 files changed

+102
-24
lines changed

any_tests/jsoniter_any_string_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@ import (
88
)
99

1010
var stringConvertMap = map[string]string{
11-
"null": "",
12-
"321.1": "321.1",
13-
`"1.1"`: "1.1",
14-
`"-123.1"`: "-123.1",
15-
"0.0": "0.0",
16-
"0": "0",
17-
`"0"`: "0",
18-
`"0.0"`: "0.0",
19-
`"00.0"`: "00.0",
20-
"true": "true",
21-
"false": "false",
22-
`"true"`: "true",
23-
`"false"`: "false",
24-
`"true123"`: "true123",
25-
`"+1"`: "+1",
26-
"[]": "[]",
27-
"[1,2]": "[1,2]",
28-
"{}": "{}",
11+
"null": "",
12+
"321.1": "321.1",
13+
`"1.1"`: "1.1",
14+
`"-123.1"`: "-123.1",
15+
"0.0": "0.0",
16+
"0": "0",
17+
`"0"`: "0",
18+
`"0.0"`: "0.0",
19+
`"00.0"`: "00.0",
20+
"true": "true",
21+
"false": "false",
22+
`"true"`: "true",
23+
`"false"`: "false",
24+
`"true123"`: "true123",
25+
`"+1"`: "+1",
26+
"[]": "[]",
27+
"[1,2]": "[1,2]",
28+
"{}": "{}",
2929
`{"a":1, "stream":true}`: `{"a":1, "stream":true}`,
3030
}
3131

extension_tests/decoder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package test
22

33
import (
4+
"bytes"
45
"github.com/json-iterator/go"
56
"github.com/stretchr/testify/require"
67
"strconv"
78
"testing"
89
"time"
910
"unsafe"
10-
"bytes"
1111
)
1212

1313
func Test_customize_type_decoder(t *testing.T) {

reflect_map.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,26 @@ func decoderOfMapKey(ctx *ctx, typ reflect2.Type) ValDecoder {
6464
return &numericMapKeyDecoder{decoderOfType(ctx, typ)}
6565
default:
6666
ptrType := reflect2.PtrTo(typ)
67-
if ptrType.Implements(textMarshalerType) {
67+
if ptrType.Implements(unmarshalerType) {
68+
return &referenceDecoder{
69+
&unmarshalerDecoder{
70+
valType: ptrType,
71+
},
72+
}
73+
}
74+
if typ.Implements(unmarshalerType) {
75+
return &unmarshalerDecoder{
76+
valType: typ,
77+
}
78+
}
79+
if ptrType.Implements(textUnmarshalerType) {
6880
return &referenceDecoder{
6981
&textUnmarshalerDecoder{
7082
valType: ptrType,
7183
},
7284
}
7385
}
74-
if typ.Implements(textMarshalerType) {
86+
if typ.Implements(textUnmarshalerType) {
7587
return &textUnmarshalerDecoder{
7688
valType: typ,
7789
}

skip_tests/struct_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func init() {
1313
`{"hello":{}}`, // valid
1414
`{"hello":{}}}`, // invalid
1515
`{"hello": { "hello": 1}}`, // valid
16-
`{abc}`, // invalid
16+
`{abc}`, // invalid
1717
},
1818
})
1919
}

value_tests/map_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package test
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"math/big"
7+
"time"
68
)
79

810
func init() {
@@ -27,6 +29,8 @@ func init() {
2729
nilMap,
2830
&nilMap,
2931
map[string]*json.RawMessage{"hello": pRawMessage(json.RawMessage("[]"))},
32+
map[Date]bool{{}: true},
33+
map[Date2]bool{{}: true},
3034
)
3135
unmarshalCases = append(unmarshalCases, unmarshalCase{
3236
ptr: (*map[string]string)(nil),
@@ -37,6 +41,20 @@ func init() {
3741
}, unmarshalCase{
3842
ptr: (*map[string]*json.RawMessage)(nil),
3943
input: "{\"test\":[{\"key\":\"value\"}]}",
44+
}, unmarshalCase{
45+
ptr: (*map[Date]bool)(nil),
46+
input: `{
47+
"2018-12-12": true,
48+
"2018-12-13": true,
49+
"2018-12-14": true
50+
}`,
51+
}, unmarshalCase{
52+
ptr: (*map[Date2]bool)(nil),
53+
input: `{
54+
"2018-12-12": true,
55+
"2018-12-13": true,
56+
"2018-12-14": true
57+
}`,
4058
})
4159
}
4260

@@ -49,3 +67,51 @@ type MyString string
4967
func (ms MyString) Hello() string {
5068
return string(ms)
5169
}
70+
71+
type Date struct {
72+
time.Time
73+
}
74+
75+
func (d *Date) UnmarshalJSON(b []byte) error {
76+
dateStr := string(b) // something like `"2017-08-20"`
77+
78+
if dateStr == "null" {
79+
return nil
80+
}
81+
82+
t, err := time.Parse(`"2006-01-02"`, dateStr)
83+
if err != nil {
84+
return fmt.Errorf("cant parse date: %#v", err)
85+
}
86+
87+
d.Time = t
88+
return nil
89+
}
90+
91+
func (d *Date) MarshalJSON() ([]byte, error) {
92+
return []byte(d.Time.Format("2006-01-02")), nil
93+
}
94+
95+
type Date2 struct {
96+
time.Time
97+
}
98+
99+
func (d Date2) UnmarshalJSON(b []byte) error {
100+
dateStr := string(b) // something like `"2017-08-20"`
101+
102+
if dateStr == "null" {
103+
return nil
104+
}
105+
106+
t, err := time.Parse(`"2006-01-02"`, dateStr)
107+
if err != nil {
108+
return fmt.Errorf("cant parse date: %#v", err)
109+
}
110+
111+
d.Time = t
112+
return nil
113+
}
114+
115+
func (d Date2) MarshalJSON() ([]byte, error) {
116+
return []byte(d.Time.Format("2006-01-02")), nil
117+
}

value_tests/raw_message_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
func init() {
88
marshalCases = append(marshalCases,
99
json.RawMessage("{}"),
10-
selectedMarshalCase{struct {
10+
struct {
1111
Env string `json:"env"`
1212
Extra json.RawMessage `json:"extra,omitempty"`
1313
}{
1414
Env: "jfdk",
15-
}},
15+
},
1616
)
1717
unmarshalCases = append(unmarshalCases, unmarshalCase{
1818
ptr: (*json.RawMessage)(nil),

0 commit comments

Comments
 (0)