diff --git a/Makefile b/Makefile index f877ab2..9c76392 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,8 @@ generate: root build .root/src/$(PKG)/tests/data.go \ .root/src/$(PKG)/tests/omitempty.go \ .root/src/$(PKG)/tests/nothing.go \ - .root/src/$(PKG)/tests/named_type.go + .root/src/$(PKG)/tests/named_type.go \ + .root/src/$(PKG)/tests/custom_map_key_type.go .root/bin/easyjson -all .root/src/$(PKG)/tests/data.go .root/bin/easyjson -all .root/src/$(PKG)/tests/nothing.go @@ -33,6 +34,7 @@ generate: root build .root/bin/easyjson -build_tags=use_easyjson .root/src/$(PKG)/benchmark/data.go .root/bin/easyjson .root/src/$(PKG)/tests/nested_easy.go .root/bin/easyjson .root/src/$(PKG)/tests/named_type.go + .root/bin/easyjson .root/src/$(PKG)/tests/custom_map_key_type.go test: generate root go test \ diff --git a/tests/basic_test.go b/tests/basic_test.go index 0186784..9476752 100644 --- a/tests/basic_test.go +++ b/tests/basic_test.go @@ -38,6 +38,7 @@ var testCases = []struct { {&IntsValue, IntsString}, {&mapStringStringValue, mapStringStringString}, {&namedTypeValue, namedTypeValueString}, + {&customMapKeyTypeValue, customMapKeyTypeValueString}, {&mapMyIntStringValue, mapMyIntStringValueString}, {&mapIntStringValue, mapIntStringValueString}, {&mapInt32StringValue, mapInt32StringValueString}, diff --git a/tests/custom_map_key_type.go b/tests/custom_map_key_type.go new file mode 100644 index 0000000..e5cc32e --- /dev/null +++ b/tests/custom_map_key_type.go @@ -0,0 +1,30 @@ +package tests + +import fmt "fmt" + +//easyjson:json +type CustomMapKeyType struct { + Map map[customKeyType]int +} + +type customKeyType [2]byte + +func (k customKeyType) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%02x"`, k)), nil +} + +func (k *customKeyType) UnmarshalJSON(b []byte) error { + _, err := fmt.Sscanf(string(b), `"%02x%02x"`, &k[0], &k[1]) + return err +} + +var customMapKeyTypeValue CustomMapKeyType + +func init() { + customMapKeyTypeValue.Map = map[customKeyType]int{ + customKeyType{0x01, 0x01}: 1, + customKeyType{0x02, 0x02}: 2, + } +} + +var customMapKeyTypeValueString = `{"Map":{"0101":1,"0202":2}}`