Commit b39c883e authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

encoding/json: allow / and % in tag names

Fixes #2718

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/5532095
parent 0d8c6b4f
...@@ -419,8 +419,13 @@ func isValidTag(s string) bool { ...@@ -419,8 +419,13 @@ func isValidTag(s string) bool {
return false return false
} }
for _, c := range s { for _, c := range s {
if c != '$' && c != '-' && c != '_' && !unicode.IsLetter(c) && !unicode.IsDigit(c) { switch c {
return false case '$', '-', '_', '/', '%':
// Acceptable
default:
if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
return false
}
} }
} }
return true return true
......
...@@ -36,6 +36,10 @@ type miscPlaneTag struct { ...@@ -36,6 +36,10 @@ type miscPlaneTag struct {
V string `json:"色は匂へど"` V string `json:"色は匂へど"`
} }
type percentSlashTag struct {
V string `json:"text/html%"` // http://golang.org/issue/2718
}
type emptyTag struct { type emptyTag struct {
W string W string
} }
...@@ -68,6 +72,7 @@ var structTagObjectKeyTests = []struct { ...@@ -68,6 +72,7 @@ var structTagObjectKeyTests = []struct {
{misnamedTag{"Animal Kingdom"}, "Animal Kingdom", "X"}, {misnamedTag{"Animal Kingdom"}, "Animal Kingdom", "X"},
{badFormatTag{"Orfevre"}, "Orfevre", "Y"}, {badFormatTag{"Orfevre"}, "Orfevre", "Y"},
{badCodeTag{"Reliable Man"}, "Reliable Man", "Z"}, {badCodeTag{"Reliable Man"}, "Reliable Man", "Z"},
{percentSlashTag{"brut"}, "brut", "text/html%"},
} }
func TestStructTagObjectKey(t *testing.T) { func TestStructTagObjectKey(t *testing.T) {
...@@ -88,7 +93,7 @@ func TestStructTagObjectKey(t *testing.T) { ...@@ -88,7 +93,7 @@ func TestStructTagObjectKey(t *testing.T) {
t.Fatalf("Unexpected value: %#q, want %v", s, tt.value) t.Fatalf("Unexpected value: %#q, want %v", s, tt.value)
} }
default: default:
t.Fatalf("Unexpected key: %#q", i) t.Fatalf("Unexpected key: %#q, from %#q", i, b)
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment